4.3 Working with Inheritance


Inheritance is a core component of the object-oriented paradigm. With the help of inheritance, it is possible to build short and efficient code. However, if the hierarchy of objects becomes too complex, your applications will be difficult to debug.

Inheritance means that a class can inherit methods and attributes from a parent class. In PHP, a class can only inherit from the parent class it is not possible for one class to have more than one parent.

After this theoretical overview, you will see an example of a class that inherits from a parent class:

 <?php # Class human class human {         var $age;         var $gender;         # Constructor         function human($age, $gender)         {                 $this->age = $age;                 $this->gender = $gender;         }         function getage()         {                 echo "age: $this->age<br>\n";                 return $this->age;         } } class child extends human {         var $mother;         var $father;         function child($age, $gender, $mother, $father)         {                 $this->age = $age;                 $this->gender = $gender;                 $this->mother = $mother;                 $this->father = $father;         }         function getfather()         {                 echo "father: $this->father<br>\n";                 return $this->father;         } } ?> 

The class called human has two attributes and one constructor. In addition, it provides a function to extract the age of a human being. getage displays the age on the screen and returns it to the calling function. The next class you can find in the code is the class called child. child extends human, which means that child inherits all attributes and functions from human. child has two more attributes than the parent. $mother and $father can be used to store the names of the parents of a child. These attributes don't have to be listed at the beginning of the class: It is clear that these attributes are part of the class because they have been inherited from the parent class. child has a constructor that assigns all attributes passed to it to the attributes of the class. The function called getfather can be used to retrieve attribute father.

Let's have a look at a script using the classes human and child:

 <?php         include("human.php");         # Generating two humans         $baby  = new child(1, 'f', 'Christina', 'Epi');         echo "<b>The father's name is ".$baby->getfather()."<br></b>\n";         echo "<b>The baby is ".$baby->getage()." years old</b><br>\n"; ?> 

First, the file containing the two classes is included. In the next step $baby is created. $baby is an instance of child. As you can see, the constructor is called with four parameters and each of these parameters is assigned to the attributes of the instance. In the next step the father of the baby and the age will be displayed on the screen. First getfather is called, which retrieves the name of the father and displays it on the screen. In the next step the age of the baby has to be displayed. Because you don't want to access $age via $baby, getage is called. As you have seen before, child does not contain the function getage. Because human is the parent class of child, child inherits all functions from human. If PHP does not find a function in a class, it will automatically look for a function with the same name in the parent class:

 father: Epi The father's name is Epi age: 1 The baby is 1 years old 

As you can see, PHP has found the appropriate function in the parent class, so it can be used for working with $baby.

In the next example you will see how a hierarchy of classes can be built. The target is to write three classes. The parent class contains some information that is inherited by a child class. The child class, however, is the parent class of an additional class:

 <?php # Class human class creature {         var $name;         function creature($name)         {                 $this->name = $name;                 echo "creature created<br><br>\n";         } } class human extends creature {         var $gender;         function human($name, $gender)         {                 $this->name = $name;                 $this->gender = $gender;                 echo "human created<br><br>\n";         } } class worker extends human {         var $profession;         function worker($name, $gender, $profession)         {                 $this->name = $name;                 $this->gender = $gender;                 $this->profession = $profession;                 echo "worker created<br><br>\n";         } } # Main program $myworker = new worker('Paul', 'male', 'chief cleaning manager'); echo "name: $myworker->name<br>\n"; echo "gender: $myworker->gender<br>\n"; echo "profession: $myworker->profession<br>\n"; ?> 

The parent class is called creature. In this model, every creature has a name. The table for storing humans can be used to store special attributes of humans. Human inherits everything from creature. Workers are special humans and therefore worker inherits everything from human and adds its own extensions. Let's execute the script:

 worker created name: Paul gender: male profession: chief cleaning manager 

As you can see, the constructor of worker is called and all values are displayed on the screen.

All classes inherit attributes and methods from a parent class. It is not possible for a parent class to inherit from a child class, as shown in Figure 4.1:

Figure 4.1. An object model.

graphics/04fig01.gif

Inheritance is a one-way street it is not possible for parents to inherit from the children.

With the help of PHP, it is possible to build complex models and to make heavy use of inheritance.



PHP and PostgreSQL. Advanced Web Programming2002
PHP and PostgreSQL. Advanced Web Programming2002
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 201

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net