Concept of Inheritance in PHP

Inheritance in PHP

It is one of the important programming principle in OOPs Concepts. It will let you to code in a more modular way by which you will be able to reuse your code. It introduces the parent and child class concepts in programming language. To implement the inheritance in PHP we use “extends” keyword.

For example, we have two class

  1. Person
  2. Employee

Now let’s decide the relationship between these two classes.

Here Person Class is parent class and Employee is a child class

Person Class is parent class because it shows more primitive information as comare to Employee class.

For example

what a Person Class shows us:-

  1. Gender : male/female.
  2. Name.
  3. Address.
  4. color.
  5. height.

What a Employee Class shows us:-

  1. Gender : male/female
  2. Name
  3. Address
  4. color
  5. height
  6. employee id
  7. employee department
  8. employee salary

These two classes are just for example otherwise it will show much information.

So let’s take close look at employee class here Gender, Name, Address, Color, Height are common with person class. So why do we define it again in Employee, it’s not good programming practice. So what we will do in this case

We define an Employee Class with three attributes only as below mentioned:-

  1. employee id
  2. employee department
  3. employee salary

And others will be inherited from Person Class.

How can we implement inheritance in PHP

class Person{
public $name;
public $gender;
public $address;
public $height;
public $color;
public $race;
}
class Employee extends Person{
public $employee_id;
public $employee_dept;
public $employee_sal;
}
$employee = new Employee();
var_dump($employee); // try this;

Types of inheritance possible with PHP

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance

Single Inheritance :

Single Inheritance consists of a single parent class and will have one child class. This kind of relationship comes from single inheritance. Above mentioned relationship between Person and Employee class is also an example of Single Inheritance.

Multilevel Inheritance :

Multilevel inheritance will have a chain of single inheritance. For example, there is parent and child relationship between Person and Employee class, Now If we define a Manager class which will inherit the employee class. Then this type of inheritance will create a Multilevel Inheritance.

class Person{
public $name;
public $gender;
public $address;
public $height;
public $color;
public $race;
}
class Employee extends Person{
public $employee_id;
public $employee_dept;
public $employee_sal;
}
class Manager extends Employee{
public $department;
public $manager_id;
public $reporting_manager;
}
$mgr1 = new Manager();
var_dump($mgr1); // try this;

Hierarchical Inheritance :

Hierarchical inheritance consists of a single parent class and that parent class is inherited by multiple child class. For Example, there is a parent class named Person and we have two child class named Employee and Student. Both these class inherit Person class. This type of the relationship comes from hierarchical inheritance.

Try out yourself for Hierarchical Inheritance.

 

Inheritance in PHP
Inheritance in PHP

Note:

  1. Multiple Inheritance is not supported in PHP
  2. We cannot use extend keyword with Final Class.
  3. Inheritance represents the IS-A relationship. For Example “Employee is a Person”.
  4. we access parent class property inside child class using parent keyword.
  5. For Interfaces, we use implement keyword to inherit its property.

Inheritance facilitates the code reusability feature which makes it one of the most important principles of OOPs Concepts.