change in the Critter class definition: Notice that the constructor no longer sets the name property directly, but uses the setName method instead. This is useful in a moment. The Inherit.php program adds some new features to the basic Critter class: <!doctype html public “-//W3C//DTD HTML 4.0 //EN”> <html> <head> <title>Glitter Critter</title> </head> <body> <? // Incorporating Inheritance //pull up the Critter class include “critter.php”; //create new Glitter Critter based on Critter class GlitterCritter extends Critter{ //add one method function glow(){ print $this->name . “ gently shimmers <br> \n”; } // end glow //override the setName method function setName($newName){ $this->name = “Glittery “ . $newName; } // end setName } // end GC class def //make an instance of the new critter $theCritter = new GlitterCritter(“Gloria”); //GC has no constructor, so it ‘borrows’ from its parent print “Critter name: “ . $theCritter->getName() . “<br>\n”; 258 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r //invoke new glow method $theCritter->glow(); ?> </body> </html> The program begins by including the previously designed Critter class. I could now make instances of that class, but I have something sneakier in mind. I want to make a new type of Critter that knows how to glow. I’ll call it the GlitterCritter. (I also wrote prototypes for the HitterCritter, BitterCritter, and SpitterCritter, but I decided not to include them in the book.) I defined the GlitterCritter just like any other class, except for the extends keyword: class GlitterCritter extends Critter{ Unless I indicate otherwise, the GlitterCritter will act just like an ordinary Critter. It automatically inherits all properties, methods, and even the con- structor from the parent class. I added two methods to the class. One brand new method is called glow(). The original Critter class doesn’t have a glow() method. The other method is called setName(). The original Critter class has a setName() method as well. When you run the program, you see a page like Figure 7.10. 259 C h a p t e r 7 W r i t i n g P r o g r a m s w i t h O b j e c t s FIGURE 7.10 The Glitter Critter has some new tricks and borrows others from the ordinary Critter. Since GlitterCritter is based on Critter and I’m making an instance of GlitterCritter, the default behavior of $theCritter is just like an ordinary Critter. Glitter-Critter doesn’t have a constructor, so it uses the constructor from Critter. When I added the glow() method, the GlitterCritter was able to do something its parent could not. When I created a new method that had the same name as a method in the parent class, the new method overrode the original method, chang- ing the behavior. Note that I didn’t change the constructor at all, but since the con- structor calls the addName() method, GlitterCritter names all begin with Glittery. Building the SuperHTML Class Now that you understand something about object-oriented methodology, you can look at the innards of the SuperHTML. Although the class has a lot of code, everything is made up of very simple code elements. The object-oriented nature of the class is what gives it its real power. As you look through the code, I give suggestions on areas you could improve the code or ways to extend the class. Setting Up the File The class file is meant to be included in other programs, so I stripped it of all unnecessary HTML and PHP code. The only thing in the file is the class definition. <? //SuperHTML Class Def class SuperHTML{ //properties var $title; var $thePage; You might be surprised that the entire SuperHTML class has only two properties. It could have a lot more, but I didn’t need them to get the basic functionality I wanted. The title property holds the page title, which appears as both the title and a level-one headline. The thePage property is special, because it is a string variable that contains all the code for the resulting HTML output. 260 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r IN THE REAL WORLD Entire books have been written about OOP. This chapter means to whet your appetite for the power and flexibility this programming style offers. I encourage you to read more about OOP and to investigate languages that support the par- adigm more completely than does PHP. Creating the Constructor You might expect a complex object to have an involved constructor, but it isn’t necessarily so. function __construct($tTitle = “Super HTML”){ //constructor $this->SetTitle($tTitle); } // end constructor The constructor copies a temporary title value over to the title property using the currently undefined setTitle() method. Manipulating Properties You would expect to find access methods for the properties, and SuperHTML has a few. There is a setTitle(), a getTitle(), and a getPage() method. However, there’s no explicit setPage() method because I intend for the programmer to build the page incrementally through all the other methods. function getTitle(){ return $this->title; } // end getTitle function setTitle($tTitle){ $this->title = $tTitle; } // end setTitle function getPage(){ return $this->thePage; } // end getPage Each of these methods is simplistic. You could improve them by checking for pos- sible illegal values or adding default values. Adding Text The SuperHTML program doesn’t print anything. All it ever does is create a big string ( thePage) and allow a programmer to retrieve that page. The addText() function adds to the end of $thePage whatever input is fed it, along with a trailing newline character. Like most of the functions in the class, a g ver- sion returns the value with a newline but doesn’t write anything to $thePage. 261 C h a p t e r 7 W r i t i n g P r o g r a m s w i t h O b j e c t s The gAddText() method isn’t necessary, but I included it for completeness. function addText($content){ //given any text (including HTML markup) //adds the text to the page $this->thePage .= $content; $this->thePage .= “\n”; } // end addText function gAddText($content){ //given any text (including HTML markup) //returns the text $temp= $content; $temp .= “\n”; return $temp; } // end addText Building the Top of the Page The top of almost every Web page I make is nearly identical, so I wanted a func- tion to automatically build that stuff for me. The buildTop() method takes a multi-line string of all my favorite page-beginning code and adds it to the page using the addText() method. function buildTop(){ $temp = <<<HERE <!doctype html public “-//W3C//DTD HTML 4.0 //EN”> <html> <head> <title>$this->title</title> </head> <body> <center> <h1>$this->title</h1> </center> HERE; $this->addText($temp); } // end buildTop; If you want a different beginning code (a particular CSS style, for example), you can override my buildTop() with one that includes your own code. 262 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r . property directly, but uses the setName method instead. This is useful in a moment. The Inherit .php program adds some new features to the basic Critter class: <!doctype html public “-//W3C//DTD. Critter</title> </head> <body> <? // Incorporating Inheritance //pull up the Critter class include “critter .php ; //create new Glitter Critter based on Critter class GlitterCritter extends Critter{ //add. class file is meant to be included in other programs, so I stripped it of all unnecessary HTML and PHP code. The only thing in the file is the class definition. <? //SuperHTML Class Def class