PHP Inheritance

Inheritance is a wonderful feature of OOPS. It is solved the duplication of code.

Code duplication happened when user uses same code multiple times. Using Inheritance declare the logic in one class and extends it whenever needs to use it. And also add new functionality with old one.

In Inheritance, there have parent or base class and a child or derived class. Write logic in parent class onetime and reuse it many times in child class.

Child class has the power to access all or a few methods and variables of the parent class. Child class has also own member variables or functions.

Child class has the power to access all or few methods and variables of the parent class. Child class has also own member variables or functions.

How to extend Parent or Base Class


Parent Class:  A class which is inherited from another class that is called parent or base or superclass.

Child Class: A class which is inherits from another class that is called child or sub or derived class.

In this above example, Animal is a parent or base class and Dog is a Child class.

Dog class can access Animal class member variable and function for Inheritance. Extends keyword helps to create a child class.

Whenever wants to inherit parent class then need to use extends keyword to create child class.

Child class cannot access all properties of parent class. In PHP there are 3 types of Access Specifiers.

Public: By default all class member variable and functions are public member.

All public members of parent class can, by child class and also out of the uses. Public member define by Public Keyword.

Private: Private member defines by Private Keyword. Only Parent class can be accessed Private property.

Private property cannot be access in child class and out of the class.

Protected: Protected Member defines by Protected Keyword. Protected Members are only access into Parent Class and its child class.

Protected property cannot access out of the class.


Five Types of Inheritance in Programming Language:

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

But in PHP only Single and Multi-level inheritance supported.

  1. Single Inheritance: There only one base class and one child class. Child class only derived from one base class.


  1. Multi-Level Inheritance: There one base class derived one child class and that child class derived another child class and so on.