While you can always modify a class to add more properties and methods, there is a better way to create a more complex class based on a simple one. Extending existing classes by creating new classes which inherit their parent class properties and methods may come in handy when you need to create more classes based on the initial one. This is also called a "parent-child" relationship, the reasons are obvious. Know that the parent class must be defined before the child class, so the order in which the classes are defined is important. Let's get back to our automobile class, and create a new class based on the original one:
class used_automobile_class extends automobile_class
{
var $owner;
var $is_price_negotiable;
function write_negotiable()
{
if ($this->is_price_negotiable)
{
print "The price is negotiable!";
}
else
{
print "The price is NOT negotiable!";
}
}
}
We have created a new class that inherits the all of the properties and methods of "automobile_class", and adds some new ones. While you can always add new properties and methods, it is not possible to remove any of the properties or methods defined in the parent class. You can override them, though. This means that a child class can redefine a method of its parent class, and any new objects created based on the child class will use the child's redefined method.
After reading this, you may wonder why you should expand existing classes, and not just rewrite the class and adapt it to the new requirements. The answer lies in flexibility. There are times when you start writing a class from scratch, add some more properties as needed and create this large class, but then you want to get back to one of the initial structures and redefine it some other way. This would not have happened if you had created children classes that expanded the original class.
If you have worked with OOP before on other programming languages, you may be wondering "how about the constructors and destructors?" If you don't know what I'm talking about, you should be interested to know that PHP allows you to automatically call a special method (also known as a constructor) when you create a new instance of the class using "new". Destructors, on the other hand, are special methods that are automatically called when an object is destroyed. Unfortunately, there are no destructors in PHP, but you may use "register_shutdown_function()" instead to simulate most effects of a destructor.
Creating a constructor is easy, you only have to create a method with the same name as the class. In the following example, we output a string and set a variable when an object is created based on this class:
class automobile_class
{
var $negotiable_price;
var $price; //the price of the car, in dollars
function automobile_class()
{
print "Object created!";
this->$negotiable_price = FALSE;
}
}
Please take notice of the fact that in PHP there are some class and methods names that are reserved to language. A small constraint is that you cannot name a class "stdClass", because this is internally used by PHP. Also, it is recommended that you don't use function names beginning with "__", they are "magical" to PHP.
No comments:
Post a Comment