<td>Joe</td> </tr> <tr> <td>options</td> <td>1</td> </tr> <tr> <td>name</td> <td>Jonathon</td> </tr> <tr> <td>address</td> <td>123 W 4th St</td> </tr> <tr> <td>phone</td> <td>999-9999</td> </tr> <tr> <td>number</td> <td>3</td> </tr> </table> </body> </html> Understanding OOP The SuperHTML project uses many OOP features to do its work. Before digging into the innards of SuperHTML itself, it makes sense to think more about what objects are and how to create them in PHP. Objects Overview As you’ve seen, objects have properties , which are characteristics of the object, and methods , which are things the object can do. In addition to supporting properties and methods, a properly designed object should reflect certain values of the object-oriented paradigm. 248 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 249 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 Many discussions of OOP indicate that objects also have events. An event is some sort of stimulus the object can respond to. Events are indeed important in OOP, but they are not often used in PHP programming, because events are meant to capture things that happen in real time. PHP programs rarely involve real-time interaction with the user, so events are not as critical in PHP objects as they are in other languages. Encapsulation An object can be seen as some data (the properties) and some code (the methods) for working with that data. Alternatively, you could see an object as a series of methods and the data that supports these methods. Regardless, you can use an object without knowing exactly how it is constructed. This principle of encapsu- lation is well supported by PHP. You take advantage of encapsulation when you build ordinary functions. Objects take the notion of encapsulation one step fur- ther by grouping together code and data. Inheritance Inheritance is the idea that an object can be inherited from another object. Imag- ine if you had to build a police car. You could build a factory that begins with sheet metal and other raw materials, or you could start with a more typical car and simply add the features that make it a police car. Inheritance involves taking an existing type of object and adding new features to create a new object type. PHP supports at least one kind of inheritance, as you see later in this chapter. Polymorphism You’ve encountered polymorphism in the SuperHTML description. Polymorphism involves an object’s ability to act somewhat differently under different circum- stances. Specifically, it is often used to handle unexpected or missing data. PHP supports some types of polymorphism, but to be honest this is more a factor of the permissive and loose variable typing of PHP than any particular object- oriented design consideration. Creating a Basic Object One of the easiest ways to understand something is to look at an example. Begin by looking at the basic critter in Figure 7.8. TRICK Of course you won’t see anything special if you look at the HTML output or the Critter.html HTML source code. The interesting work was done in the php code: <!doctype html public “-//W3C//DTD HTML 4.0 //EN”> <html> <head> <title>Critter</title> </head> <body> <? // BASIC OOP DEMO //define the critter class class Critter{ var $name; } // end Critter class //make an instance of the critter $theCritter = new Critter(); //assign a value to the name property $theCritter->name = “Andrew”; 250 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 FIGURE 7.8 The Critter is a simplistic animal, but it is a simple example of object- oriented design. //return the value of the name property print “My name is “; print $theCritter->name; ?> </body> </html> Defining the SimpleCritter Class The SimpleCritter program works in classic object-oriented style. First it defines what a critter is and then it creates an instance of that design. Consider this part of the code: //define the critter class class Critter{ var $name; } // end Critter class The class keyword indicates that I am defining a class. A class is a design or tem- plate for something. A recipe is a good example of a class. You wouldn’t actually eat the index card with the cookie recipe on it, but you use that recipe to create cookies, which you can eat. The recipe is the class and cookies are instances of that class. (Great. Now I’m hungry.) When I defined the Critter class, I was defining what a critter would be like (the recipe), but I haven’t made one yet (the cookie). My Critter class is extremely simple. Right now it only has one property, which is the variable $name. Class def- initions get a lot more complicated, but this is a good start. Note the use of the var keyword to specify an instance variable. You don’t have to use the var keyword when you create ordinary variables in PHP (and almost nobody does). The var keyword is necessary in a class definition, or the variable will not be interpreted correctly. Creating an Instance of the Critter Class Once you’ve defined a class, you want to have an instance or two of that class. One of the great things about objects is how easily you can make multiple instances of some class. However, for this example you just make one instance. The code that creates an instance looks like this: $theCritter = new Critter(); TRAP 251 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 252 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 I created a new variable called $theCritter and used the new keyword to indicate I wanted to instantiate some sort of object. Of course, I made an instance of the Critter class. It’s traditional to begin class names with uppercase letters and instances (like most other variables) in lowercase letters. I follow that convention through this book, so $theCritter is an instance and Critter is a class. In PHP, it’s also easy to see that Critter isn’t a variable because it doesn’t begin with a dollar sign. Working with an Object’s Properties Once you have an instance of an object, you can manipulate the properties of that instance. The $theCritter variable is an instance of the Critter class, so I can assign a value to the name property of $theCritter. //assign a value to the name property $theCritter->name = “Andrew”; Notice a couple of things about this: • You can attach values to instances of a class, not to the class itself. • Look carefully at the syntax for assigning a value to the name property. The variable you are dealing with is $theCritter. The name property is kind of like a subvariable of $theCritter. Use the instance->property syntax to refer to an object’s property. It’s actually considered dangerous to directly access a property as I’m doing in this example. However, I do it here for the sake of clarity. As soon as I show you how to create a method, you’ll build access methods. That way you don’t have to directly access properties. Retrieving Properties from a Class The basic syntax for retrieving a property value from a class is much like adding a property. //return the value of the name property print “My name is “; print $theCritter->name; Again, note the syntax: $theCritter is the variable and name is its property. TRAP TRICK . in PHP programming, because events are meant to capture things that happen in real time. PHP programs rarely involve real-time interaction with the user, so events are not as critical in PHP. handle unexpected or missing data. PHP supports some types of polymorphism, but to be honest this is more a factor of the permissive and loose variable typing of PHP than any particular object- oriented. without knowing exactly how it is constructed. This principle of encapsu- lation is well supported by PHP. You take advantage of encapsulation when you build ordinary functions. Objects take the notion