Skip to main content

Inheritance in PHP

Inheritance is a concept in object oriented programming. With the help of inheritance we can get all property and method of one class in another class. This is principle to take re-fusibility on upper level.  Inheritance in php is introduced from php5 version.

In this chapter we will explore about basics concept of inheritance  After basic we will discuss implementation of inheritance in php. This tutorial for the beginner who want to learn basic concept of inheritance in php. Before going further I am assuming that have some idea of oop in php. Later in this chapter we will cover some advance aspect of the inheritance also.

What is inheritance?

Inheritance is nothing but a design principle in oop. By implementing inheritance you can inherit(or get) all properties and methods of one class to another class.The class who inherit feature of another class known as child class.The class which is being inherited is know as parent class.Concept of the inheritance in oop is same as inheritance in real world. For example, child inherits characteristics  of their parent. Same is here in oop. One class is inheriting characteristics of another class.

With the help of inheritance you can increase re-usability of code. Let us take an example in terms of generic programming practices. Suppose you are going to create classes to render different html tag(div, span, form, table etc).  Now you will create class with name html_div, html_span ,  html_form. You are creating different class because every element is different in nature. For example form has action and method and you will have different input element in form. But table will have tbody, tr, th and td.

Now just think for some moment. There are some element and their rendering is same in all element. For example all html mention above is having name, id, class attribute which is same. Also rendering of those element is also same. So in above case you can create parent class with name HTML and you can inherit that class across all of your classes like div, span, form. Following is the generic code structure of inheritance in oop taking your HTML attribute in consideration. I am taking php syntex for better understnding
class HTML
{
protected $name;
protected $id;
protected function basicAttribute
{
return "name='$this->name' id='$this->id'";
}
}
Class HTML_div extends HTML
{
public function __construct($id , $name)
{
$this->id = $id;
$this->name = $name;
}
public function getDiv($content)
{
$basicAttribute = $this->basicAttribute();
return "<div $basicAttribute >$content</div>"
}
}
Class HTML_span extends HTML
{
public function __construct($id , $name)
{
$this->id = $id;
$this->name = $name;
}
public function getSpan($content)
{
$basicAttribute = $this->basicAttribute();
return "<span $basicAttribute >$content</span>"
}
}

Above code is and example of basic inheritance in php. All method(protected and public) from HTML class is directly accessible in your class HTML_div and HTML_span class. In both child classes you no need to write rendering of id and name logic again and again. This really saves time and give some good modulations in the code.
Hope your basic understanding about inheritance is clear. Now let us move to implementation of inheritance in php.

Inheritance in php

Concept of inheritance in php is as simple as in other oop languages as from php5 community target is to provide healthy oop concept. If you will analyze basic code of my previous topic, this is typical example of inheritance in php. To implementing inheritance in php you need at least 2 classes. One will be parent class and other will be child class. In child class you can inherit all properties and methods(protected and public only) from parent class. You can implement inheritance in php using keyword extends. Let us take above example again with some modification:
class HTML
{
protected $name;
public $id;
private $with;
protected function basicAttribute
{
return "name='$this->name' id='$this->id'";
}
}
Class HTML_div extends HTML
{
public function __construct($id , $name)
{
$this->id = $id;
$this->name = $name;
}
public function getDiv($content)
{
$basicAttribute = $this->basicAttribute();
return "<div $basicAttribute >$content</div>"
}
}'
$objDiv = new HTML_div("bloc_main" , 'avc');
$objDiv->getDiv('this is and example of inheritance in php');

Now in above code class HTML_div is inheriting property and method from class HTML. 

Multilevel and Multiple inheritance in PHP

In php multilevel inheritance is possible but multiple inheritance is not possible. In simplified terms in php child class can not inherit more than one parent class. But hierarchical inheritance is possible in php. Hierarchical means Parent inherit property of grand parent class. Grand child inherit property of parent class. So in multilevel inheritance child can get some property of from grand parent class also.

Example of Multiple inheritance in PHP
class test
{
//Your class body
}
class test1
{
//Your class body
}
class test3 extends test1 test2
{
//your class body
}

Above code will not work in php. Because php is single inheritance language.

Example of Multilevel inheritance in PHP
class grandParent
{
//Body of your class
}
class parent extends grandParent
{
//Body Of your class
}
class child extends parent
{
//Body of your class
}

This is very basic example of multilevel inheritance. In php it is possible to implement multilevel inheritance. In above example parent class is inheriting grand parent property. And and child is inheriting parent property. So child have some parent and grand parent property.

Static Methods and Property in Inheritance in PHP

As in our example of HTML_div class we have explored that we can use $this-> keyword to get all property and method of parent(HTML) class. But if your parent or child method is static, then you can access static methods or properties using self and parent keyword. Also this is not necessery to make method static if you want to use self or parent keyword. This is very useful if your parent and child both method is having property or method with same name. If both classes having same property and you want to call specific property or method then you can use this keyword.

Self and parent in case of static methods:
class parent
{
public static abc()
{
//your function body
}
}
class child
{
public static xyz()
{
//your function body
}
function callStatic()
{
self::xyz();
parent::abc();
}
}

Self and Parent without static
class parent
{
protected function xyz()
{
//Your function body
}
}
class child extends parent
{
public function xyz()
{
//your function body
}
public function calll()
{
self::xyz();
parent::xyz();
}
}

Download Code for Inheritance in PHP

Further details about inheritance in PHP you can read:
http://php.net/manual/en/language.oop5.inheritance.php
http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29

Next >> Abstract Classes and Interface

Support Me by Sharing This Article

Ankur Kumar Singh

I am a PHP programmer having some knowledge about Linux. I am always interested in web development and knowledge sharing. I am full time tech evangelist part time human being. :-)

14 thoughts on “Inheritance in PHP

Leave a Reply

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

shares