Inheritance is a well-established programming principle, and PHP makes use of this principle in its object model. This principle will affect the way many classes and objects relate to one another.
For example, when extending a class, the subclass inherits all of the public and protected methods, properties and constants from the parent class. Unless a class overrides those methods, they will retain their original functionality. This is useful for defining and abstracting functionality, and permits the implementation of additional functionality in similar objects without the need to reimplement all of the shared functionality.
In PHP we found inheritances like :
- Single inheritance & Multilevel inheritance
- Hybrid inheritance

- Single inheritance
Single inheritance is a concept in PHP in which one class can be inherited by a single class only. We need to have two classes in between this process. One is the base class (parent class), and the other a child class itself.
File – code1.php
<?php class father{ function house(){ echo “house owner is father!”; } } class child extends father{ } // accessing class ‘father’ methods using child class object ‘c1’ $c1=new child(); $c1->house(); ?> |

- Multilevel inheritance
Multiple Inheritances is the property of the Object Oriented Programming languages in which child class or sub class can inherit the properties of the multiple parent classes or super classes.
File – code2.php
<?php class father{ function house(){ echo “<br>house owner is father!”; } } class child extends father{ function toy(){ echo “<br>child has toy!”; } } class grandchild extends child{ } // accessing class ‘father’ & ‘child’ methods using ‘grandchild’ class object ‘bunny’ $bunny = new grandchild(); $bunny->toy(); $bunny->house(); ?> |

Hybrid inheritance
Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance. A class is derived from two classes as in multiple inheritances. However, one of the parent classes is not a base class. It is a derived class.
File – code3.php
<?php class A{ function msg(){ echo “this is class A”; } } class AB extends A{ function msg2(){ echo “<br> this is class AB extends parent class A “; } } class ACP extends AB{ function msg3(){ echo “<br> child class ACP extends parent class AB,grant parent A”; } } class AC extends A{ function msg4(){ echo “<br> child class AC extends parent class A “; } } class kali extends AC{ function msg5(){ echo “<br> child class kali parent AC, grant parent A”; } } class arch extends AC{ function msg6(){ echo “<br> child class arch parent AC, grand parent A”; } } class nmap extends kali{ function msg7(){ echo “<br> child class nmap, parent class kali,grant parent AC,A”; } } class johnripper extends kali{ function msg8(){ echo “<br> child class johnripper, parent class kali,grand-parent AC,A”; } } $john= new johnripper(); $john->msg8(); $john->msg5(); $john->msg(); $acp= new ACP(); $acp->msg3(); $acp->msg2(); ?> |
