157 Writing the Code for Your Class var $title = 'TLA Consulting Pty Ltd'; Most commercial Web pages include metatags to help search engines index them. In order to be useful, metatags should probably change from page to page.Again, we will provide a default value: var $keywords = 'TLA Consulting, Three Letter Abbreviation, some of my best friends are search engines'; The navigation buttons shown on the original page in Figure 5.2 (see the previous chapter) should probably be kept the same from page to page to avoid confusing people, but in order to change them easily, we will make them an attribute too. Because there might be a variable number of buttons, we will use an array, and store both the text for the button and the URL it should point to. var $buttons = array( 'Home' => 'home.php', 'Contact' => 'contact.php', 'Services' => 'services.php', 'Site Map' => 'map.php' ); In order to provide some functionality, our class will also need operations.We can start by providing accessor functions to set and get the values of the attributes we defined. These all take a form like this: function SetContent($newcontent) { $this->content = $newcontent; } Because it is unlikely that we will be requesting any of these values from outside the class, we have elected not to provide a matching collection of Get functions. The main purpose of this class is to display a page of HTML, so we will need a func- tion.We have called ours Display(), and it is as follows: function Display() { echo "<html>\n<head>\n"; $this -> DisplayTitle(); $this -> DisplayKeywords(); $this -> DisplayStyles(); echo "</head>\n<body>\n"; $this -> DisplayHeader(); $this -> DisplayMenu($this->buttons); echo $this->content; $this -> DisplayFooter(); echo "</body>\n</html>\n"; } 08 525x ch06 1/24/03 3:35 PM Page 157 158 Chapter 6 Object-Oriented PHP The function includes a few simple echo statements to display HTML, but mainly con- sists of calls to other functions in the class. As you have probably guessed from their names, these other functions display parts of the page. It is not compulsory to break functions up like this.All these separate functions might simply have been combined into one big function.We separated them out for a number of reasons. Each function should have a defined task to perform.The simpler this task is, the easi- er writing and testing the function will be. Don’t go too far—if you break your program up into too many small units, it might be hard to read. Using inheritance, we can override operations.We can replace one large Display() function, but it is unlikely that we will want to change the way the entire page is dis- played. It will be much better to break up the display functionality into a few self- contained tasks and be able to override only the parts that we want to change. Our Display function calls DisplayTitle(), DisplayKeywords(), DisplayStyles(), DisplayHeader(), DisplayMenu(), and DisplayFooter().This means that we need to define these operations. One of the improvements of PHP 4 over PHP 3 is that we can write operations or functions in this logical order, calling the operation or function before the actual code for the function. In PHP 3 and many other languages, we need to write the function or operation before it can be called. Most of our operations are fairly simple and need to display some HTML and per- haps the contents of our attributes. Listing 6.1 shows the complete class, which we have saved as page.inc to include or require into other files. Listing 6.1 page.inc—Our Page Class Provides an Easy Flexible Way to Create TLA Pages <?php class Page { // class Page's attributes var $content; var $title = 'TLA Consulting Pty Ltd'; var $keywords = 'TLA Consulting, Three Letter Abbreviation, some of my best friends are search engines'; var $buttons = array( 'Home' => 'home.php', 'Contact' => 'contact.php', 'Services' => 'services.php', 'Site Map' => 'map.php' ); // class Page's operations function SetContent($newcontent) { 08 525x ch06 1/24/03 3:35 PM Page 158 159 Writing the Code for Your Class $this->content = $newcontent; } function SetTitle($newtitle) { $this->title = $newtitle; } function SetKeywords($newkeywords) { $this->keywords = $newkeywords; } function SetButtons($newbuttons) { $this->buttons = $newbuttons; } function Display() { echo "<html>\n<head>\n"; $this -> DisplayTitle(); $this -> DisplayKeywords(); $this -> DisplayStyles(); echo "</head>\n<body>\n"; $this -> DisplayHeader(); $this -> DisplayMenu($this->buttons); echo $this->content; $this -> DisplayFooter(); echo "</body>\n</html>\n"; } function DisplayTitle() { echo '<title> $this->title </title>'; } function DisplayKeywords() { echo "<META name=\"keywords\" content=\"$this->keywords\">"; } function DisplayStyles() { ?> Listing 6.1 Continued 08 525x ch06 1/24/03 3:35 PM Page 159 160 Chapter 6 Object-Oriented PHP <style> h1 {color:white; font-size:24pt; text-align:center; font-family:arial,sans-serif} .menu {color:white; font-size:12pt; text-align:center; font-family:arial,sans-serif; font-weight:bold} td {background:black} p {color:black; font-size:12pt; text-align:justify; font-family:arial,sans-serif} p.foot {color:white; font-size:9pt; text-align:center; font-family:arial,sans-serif; font-weight:bold} a:link,a:visited,a:active {color:white} </style> <?php } function DisplayHeader() { ?> <table width="100%" cellpadding ="12" cellspacing ="0" border ="0"> <tr bgcolor ="black"> <td align ="left"><img src = "logo.gif"></td> <td> <h1>TLA Consulting Pty Ltd</h1> </td> <td align ="right"><img src = "logo.gif"></td> </tr> </table> <?php } function DisplayMenu($buttons) { echo "<table width='100%' bgcolor='white' cellpadding='4' cellspacing='4'\n"; echo " <tr>\n"; //calculate button size $width = 100/count($buttons); while (list($name, $url) = each($buttons)) { $this -> DisplayButton($width, $name, $url, !$this->IsURLCurrentPage($url)); } echo " </tr>\n"; echo "</table>\n"; } Listing 6.1 Continued 08 525x ch06 1/24/03 3:35 PM Page 160 161 Writing the Code for Your Class function IsURLCurrentPage($url) { if(strpos( $GLOBALS['SCRIPT_NAME'], $url )==false) { return false; } else { return true; } } function DisplayButton($width, $name, $url, $active = true) { if ($active) { echo "<td width ='$width%'> <a href ='$url'> <img src ='s-logo.gif' alt ='$name' border ='0'></a> <a href ='$url'><span class='menu'>$name</span></a></td>"; } else { echo "<td width ='$width%'> <img src ='side-logo.gif'> <span class='menu'>$name</span></td>"; } } function DisplayFooter() { ?> <table width = "100%" bgcolor ="black" cellpadding ="12" border ="0"> <tr> <td> <p class="foot">© TLA Consulting Pty Ltd.</p> <p class="foot">Please see our <a href ="">legal information page</a></p> </td> </tr> </table> <?php } } ?> Listing 6.1 Continued 08 525x ch06 1/24/03 3:35 PM Page 161 . Object-Oriented PHP <style> h1 {color:white; font-size:24pt; text-align:center; font-family:arial,sans-serif} .menu {color:white; font-size:12pt; text-align:center; font-family:arial,sans-serif;. font-family:arial,sans-serif; font-weight:bold} td {background:black} p {color:black; font-size:12pt; text-align:justify; font-family:arial,sans-serif} p.foot {color:white; font-size:9pt; text-align:center; font-family:arial,sans-serif;. DisplayKeywords(); $this -& gt; DisplayStyles(); echo "</head> <body> "; $this -& gt; DisplayHeader(); $this -& gt; DisplayMenu($this->buttons); echo $this->content; $this -& gt; DisplayFooter(); echo