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. Inheritance in php is introduced from php5 version.

Here I will explore about basics concept of inheritance  After basic we will discuss implementation of inheritance in php. 

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 tagsDetails and you can inherit that class across all of your html tags like div, span, form. Following is the generic code structure of inheritance in oop taking your HTML attribute in consideration.

Above code is and example of basic inheritance in php. All method(protected and public) from tagDetails class is directly accessible in your class page class. In child class 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.

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

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

Example of Multilevel inheritance in PHP

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 page class we have explored that we can use $this-> keyword to get all property and method of parent(tagsDetails) 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 necessary 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:

Self and Parent without static