only for RuBoard - do not distribute or recompile |
The basic definition of a class has been discussed in the Types section in this appendix. In formal class definition, a function is known as a method, and a variable is known as an attribute. When referencing variables in a class, you use the reference operator ( -> ), and you leave off the $ in front of variables.
As in other object-oriented languages, you can use other classes when creating new classes. In formal class terminology, you derive the new class from the former class. All the methods and attributes of the former class are available to you. This is called inheritance. The new class inherits the attributes and functions from the base class. The new ones are added to the list. To do this you use the keyword extends when defining the class.
Suppose you have a dog class. This dog class has ears, eyes, and a nose. You want to use this base class because it is so useful, but you need to add a tail and a function to set the length of the tail. The code might look something like this:
class BaseDogClass { var $eyes; var $ears; var $nose; function SetEyeColor($color) { $this->eyes = $color; } function SetEarSize($size) { $this->ears = $size; } function SetNoseTemperature($temperature) { $this->nose = $temperature; } } $HisDog = new BaseDogClass; class DogWithTailClass extends BaseDogClass { var $tail; function SetTailLength($length) { $this->tail = $length; } } $MyDog = new DogWithTailClass;
In the previous code, $HisDog is an object and can be used. $MyDog is an object with all the methods and attributes of $HisDog. However, $MyDog has a $tail and $HisDog does not!
NOTE
Note the use of $this. The variable $this is a reference to the attributes and methods of the object the code is currently in, and must be used to reference those methods or attributes. The $this variable is automatically defined by PHP when an object is created.
In formal class design, classes have functions that are called when the object is created using new. These functions are called constructors. Whenever a function exists that has the same name as the class, it becomes the constructor for that class. For example:
class MyNewDogClass extends BaseDog { var $tail; function MyNewDogClass() { // this is the constructor for this class $this->tail = 3; // the size of the tail is 3 inches by default $this->eyes = "brown"; $this->ears = "short"; $this->nose = "cold"; } } $MyNewDog = new MyNewDogClass;//3 inch tail, brown eyes, short ears, cold nose
The $MyNewDog just created has brown eyes and a cold nose among other things. This is a very powerful concept to be used with objects. It can make your life much easier.
NOTE
When you have derived a class from another class, the constructor in the base class is not called automatically in PHP 3. (In PHP 4, if there is no constructor for the derived class, the constructor for the parent class is called.) You must call the base class constructor yourself. Let's assume BaseDog class has a constructor. To call it, you must put the BaseDog call in your derived class. For example, the following calls the BaseDog class constructor when the object is created:
class ExampleDog extends BaseDog { var $MyVar; function ExampleDog() { $this->BaseDog(); // call base class constructor $this->MyVar = 1; } }
You can also have an array that contains objects. You might be able to call the object directly from the array, if you are using PHP 3. PHP 4 handles this situation properly. If you have problems, the following code can be of some help:
$myArray[10] = new BaseDog; $myArray[10]->SetEyeColor("red"); // this may fail in PHP 3 $tempObj = $MyArray[10]; // make a copy of the object $tempObj->SetEyeColor("red"); // this will work instead, but updates the copy
The $tempObj variable is a copy of the object in $myArray. The variable being set is in the $tempObj object, not in the $myArray object. In PHP 4, the $myArray[10]->SetEyeColor() call would work.
only for RuBoard - do not distribute or recompile |