Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In addition to offering code hinting for PHP language elements, Dreamweaver CS5 also gives you code hinting for custom classes. A PHP custom class is a way of creating reusable code. I'll start by creating a custom class, and then showing you how to use code hinting to use its features. I will be working with the version of files in the exercise files, in a folder named 03_managingcode > 02_hintingwithclasses. I will start in my _includes folder.
I am going to create a custom class that encapsulates, or contains code to output the current time to the web site. I will go to the _includes folder and create a new PHP file. I will right-click on the folder and select new file, and I will name the new file DateUtility.php. When you create a file to contain a custom class declaration, you should always use an uppercase initial character, such as the D in DateUtility. That's a convention that PHP developers use.
Now I will open the file, and just as with a Server Side Include, I will delete any existing HTML code. Just as with all PHP code, a PHP class declaration must be placed within a code block, so I will go to the Insert panel to the PHP category, and I will add a code block. Then, with the cursor inside the code block, I will press Enter a few times. Here is how you define a PHP custom class.
You start with the word "class," then you type in the name of the class. By strong convention, the name of the class matches the name of the file, but without the .php extension. Then you put in a pair of curly braces. Within the class, you can define variables or properties, and functions or methods. I am going to define a single function named getCurrentTime. I will start with the word "public," then "function," then the name of the function, "getCurrentTime." After the name with the function, I will type in a beginning and an ending parenthesis, and then I will type in another pair of curly braces.
The purpose of the getCurrentTime function will be to output the current time to the browser. I already have a little bit of code that does that that I created in a previous video. In this file set, I will find it in the phpinfo.php file. I will open that file, and then I will select and copy the three lines of code that set the current time zone and echo the current time to the browser. I will copy that code to the browser, then I will return to my class declaration, I will press Enter and then Paste. And I will select the two lines of code that aren't properly indented, then press Tab a couple of times to indent them within the function.
So now this class has a public function that I can call anytime. I can use this class anywhere in my web site, and I will always get the same formatted value. I will save the file, and then I will open the file, footer.php. In order to use a class declaration, you first have to include it. So I will place the cursor up at the top of the file and make a couple of lines of empty space. Then I will go to the Insert panel, to the PHP category, and I will add an Include command.
And I will include the file, DateUtility.php. If you want to include this file from a file in another directory, you can use a relative location, such as dots, dot-dots and names of folders. But because this file is in the same folder as footer.php, I just need to provide the file a name. I will save the file and move down to the bottom of the page. I will place the cursor after the paragraph tag with a class of phone, and I will create another pair of paragraph tags.
I will make a little bit of empty space. Then I will go to the Insert panel and add a code block, and I will make a little bit of empty space within the code block. I am going to create a variable which is an instance of my custom class. The name of the variable will be dateutil, with the dollar sign prefix. Then to create an instance of my custom class, I will use this syntax: = new, and there is my custom class name, DateUtility. I will press Enter to select in and type in an opening and a closing parenthesis and semicolon.
The dateutil variable is now an instance of my custom class, and I can call that custom class's functions, or methods, like this. I will go down to the next line, and I will type in $date. Dreamweaver shows me the name of the variable starting with date, dateutil, and I will press Enter or Return to select it. Then I am going to call a function, which is a member of the class. To call a member of the class, you use this syntax: ->. It looks like an arrow.
Dreamweaver automatically offers the name of the function in the class, getCurrentTime, using its code hinting capabilities. I will press Enter and then add a semicolon at the end of the line. So here are all the parts of the puzzle. At the top of the page, I used an Include command to include the class declaration, and then where I want the output to happen, I create an instance of the class and then finally call its function, and Dreamweaver, with its code hinting capability, helped me correctly name the class, and its function.
I will save the change to footer.php. Then I will go to one of my top-level pages. I will use index.php, which is already including footer.php at the bottom of the file. I will look at the page using Live View, updating the files on the testing server as I go. I will look in Design View, scroll down to the bottom, and there is the result: the current time. So that's a look at how you can use code hinting to work with your own custom classes.
Custom classes give you a way of encapsulating commonly used, or reusable code, and Dreamweaver CS5's PHP code hinting will help you make sure that you refer to the class names and its properties and functions correctly as you do your hand coding.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 74494 Viewers
80 Video lessons · 129709 Viewers
52 Video lessons · 63949 Viewers
59 Video lessons · 49731 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.