Objects Overview As you’ve seen, objects have properties, which are characteristics of the object, In addition to supporting properties and methods, a properly designed object should re
Trang 1<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 SuperHTMLproject uses many OOP features to do its work Before digging into the innards of SuperHTMLitself, 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,
In addition to supporting properties and methods, a properly designed object should reflect certain values of the object-oriented paradigm
248
l u
Trang 2j e
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 togefur-ther code and data
Inheritance
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 SuperHTMLdescription 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
T R I C K
Trang 3Of course you won’t see anything special if you look at the HTML output or the Critter.htmlHTML source code The interesting work was done in the phpcode:
<!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
l u
FIGURE 7.8
The Critter is a
simplistic animal,
but it is a simple
example of
object-oriented design.
Trang 4//return the value of the name property
print “My name is “;
print $theCritter->name;
?>
</body>
</html>
Defining the SimpleCritter Class
The SimpleCritterprogram 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 classkeyword indicates that I am defining a class A classis 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 Critterclass, I was defining what a critter would be like (the
recipe), but I haven’t made one yet (the cookie) My Critterclass 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();
T R A P
j e
Trang 5l u
I created a new variable called $theCritterand used the newkeyword to indicate
I wanted to instantiate some sort of object Of course, I made an instance of the Critterclass
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 $theCrittervariable is an instance of the Critterclass, so I can assign a value to the nameproperty of $theCritter
//assign a value to the name property
$theCritter->name = “Andrew”;
Notice a couple of things about this:
• You can attach values to instancesof a class, not to the class itself
• Look carefully at the syntax for assigning a value to the nameproperty The variable you are dealing with is $theCritter The nameproperty 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: $theCritteris the variable and nameis its property
T R A P
T R I C K