Inheritance in PHP

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

The process of deriving or making a new class from old class is known as inheritance .

syntax for decleration of child or new class :-

class childClassName extends ParentClassName
{
    
        statement ;
    
}

Benifits of inheritance in PHP
  1. The code is being re-used from one class to another. We need not required to re-write the same thing again and again.
  2. Extensibility is one of the advantages of the inheritance in which we can extend the base class feature without making little or no changes to fulfill the business requirements.
  3. we don’t have to describe similar property or method in upcoming class we can extends all the property of parent class using inheritance.

Types of Inheritance

  1. Single Inheritance
  2. Multi-layer inheritance
  3. hierarchical inheritance

  • Single Inheritance
  • If single class is derived or made from one parent or old class is known as Single Inheritance

    eg:-

  • Multi-layer inheritance
  • In the multi-level inheritance, we will inherit the one base class into a derived class, and then the derived class will become the base class for another derived class.

    Inheritance in php

  • hierarchical inheritance
  •  In the hierarchical inheritance, we will inherit the one base class into multiple derived classes.