PHP Inheritance

Inheritance is one of the popularly used Object Oriented Programming feature to have shared properties and functions between related classes. This feature provides following functionalities, such as,

  • Inter relating classes.
  • Providing functionalities in abstract manner.
  • Increasing reusability.

One of the noticeable change while migrating from PHP version 4 to PHP version 5 is of introducing Object oriented Programming feature as we have seen about Growth of PHP. Among the limited set of OOPS features supported by PHP, inheritance is an attractive concept by providing above functionalities listed.

php_inheritance

Before getting into PHP inheritance, let us refresh the idea about inheritance and its types in general. As per this principle, a class can be derived from another class, whereas, the classes derived is called as child or sub class and the other is called as parent or super class. The super class has its own properties and functions which can be derived by the sub class, and added to that, the sub class can have its own properties.

Now the advantages of inheritance is that there is no need to redefine the required properties of super class one again in sub class, since they could be inherited. That what, we have said that, inheritance increases reusability and reduces line of code and there by it increases simplicity among inter related classes.

Types of PHP Inheritance

Generally, inheritance has three types, single, multiple and multi level inheritance. But, PHP supports only single inheritance, where, only one class can be derived from single parent class. Even though, PHP is not supporting any other inheritance type except single inheritance, we can simulate multiple inheritance by using PHP interfaces.

In PHP, inheritance can be done by using extends keyword, meaning that, we are extending the derived class with some additional properties and methods of its parent class. The syntax for inheriting a class is as follows.

Child_class_name extends Parent_class_name {
...
}

Note: We can inherit parent class properties and function if and only if they have public or protected access modifiers.

PHP Single Inheritance

For implementing single inheritance concept in PHP, we require two classes one as parent and the other as child. Let us have two such classes named Toys and SoftToys, respectively as shown below.

Class Toys:

class Toys {
public $categories = array("puzzles","pull back","remote","soft");
public $toys = array(array("name"=>"Mechanical Cars","category"=>"pull back"),
array("name"=>"Jigsaw","category"=>"puzzles"),
array("name"=>"HiTech Cars","category"=>"remote"),
array("name"=>"Teddy Bears","category"=>"soft"),
array("name"=>"Baby pillow","category"=>"soft"),
array("name"=>"Chinese Checker","category"=>"puzzles"),
array("name"=>"Jumbo Helicopter","category"=>"remote"));
function getToys() {
for($i=0;$i<count($this->toys);$i++) {
$toys_list[] = $this->toys[$i]["name"];
}
return $toys_list;
}
function getToysByCategory($category) {
for($i=0;$i<count($this->toys);$i++) {
if($this->toys[$i]["category"]==$category) 
$toys_list[] = $this->toys[$i]["name"];
}
return $toys_list;
}
}

This class includes properties like list of categories into an array, and also have entire toys information into a associative array. After that, getToys() is used to get all toys, where as, getToysByCategory() returns list of toys by its category passing as an argument.

Class SoftToys:

class SoftToys extends Toys {
protected $category = "soft";
function getToys() {
return $this->getToysByCategory($this->category);
}
}

This class is derived from Toys class and overriding its getToys() method to return only the list of soft toys.

After defining two classes, now, it the time to access their properties and functions through the instance of these classes, to see the effect of inheritance. For that, class instances are created as follows, outside class definition.

$objToys = new Toys();
$objSoftToys = new SoftToys();

And the properties and functions are accessed by using the following code.

print "<pre>";
print_r($objToys->categories);
print_r($objSoftToys->categories);
print_r($objToys->getToys());
print_r($objSoftToys->getToys());
print "</pre>";

Since SoftToys class is derived from Toys class, accessing categories property using either class instance will print same array of result as,

Array
(
    [0] => puzzles
    [1] => pull back
    [2] => remote
    [3] => soft
)

But, getToys() function is overrided in child class. So, accessing this method by using Toys or SoftToys class instance will print different set of toys list to the browser as shown below.

Array
(
    [0] => Mechanical Cars
    [1] => Jigsaw
    [2] => HiTech Cars
    [3] => Teddy Bears
    [4] => Baby pillow
    [5] => Chinese Checker
    [6] => Jumbo Helicopter
)
Array
(
    [0] => Teddy Bears
    [1] => Baby pillow
)

This PHP tutorial was added on June 18, 2013.

«

»