Inheritance in PHP

What is Inheritance ?

Inheritance is one of the popularly used Object Oriented Programming features. It allows having shared properties and functions between related classes. Using inheritance child inherit the properties of the parent class. this is concerned with the relationship between classes. The relationship takes the form of a parent and child. The child uses the methods defined in the parent class. The main purpose of inheritance is Re-usability : a number of children, can inherit from the same parent. This is very useful when we have to provide common functionality such as adding, updating and deleting data from the database.

The effect of inheritance is that the child class has the following characteristics :

  1. Automatically has all the member variable declarations of the parent class.
  2. Automatically has all the same member functions as the parent, which (by default) will work the same way as those functions do in the parent.

Types of Inheritance in PHP

Inheritance has three types, single, multiple and multi-level inheritance. But, PHP supports single inheritance and multi-level inheritance. That means the subclass will be derived from a single parent class. Even though PHP is not supporting any multiple inheritance, we can simulate it by using PHP interfaces.

In PHP, inheritance can be implemented by using extends keyword, meaning that, we are extending the derived class with some additional properties and methods of its parent class.

Syntax
Child_class_name extends Parent_class_name {
...
}
Example :
<?php
class Books {
/* Member variables */
var $price;
var $title;

/* Member functions */
function setPrice($par){
$this->price = $par;
}

function getPrice(){
echo $this->price ."<br/>";
}

function setTitle($par){
$this->title = $par;
}

function getTitle(){
echo $this->title ." <br/>";
}
}

//Single inheritance

class Novel extends Books {
var $publisher;

function setPublisher($par){
$this->publisher = $par;
}

function getPublisher(){
echo $this->publisher. "<br />";
}
}
?>

Implementation of single inheritance :

Parent class :
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"),

function getToys() {
for($i=0;$i<count($this->toys);$i++) {
$toys_list[] = $this->toys[$i]["name"];
}
return $toys_list;
}

}
Child class :
class SoftToys extends Toys {
function getToys() {
return $this->getToys();
}
}
Creating object
$objToys = new Toys();    //Object of parent class
$objSoftToys = new SoftToys();    //Object of child class

properties and functions are accessed by using the following code.

print_r($objToys->getToys());
print_r($objSoftToys->getToys());

And both will print the same result :

Array
(
[0] => Mechanical Cars
[1] => Jigsaw
)
-WPGenius Solitions

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.