Skip to main content

In PHP, there are three types of inheritance:

  1. Single inheritance: Single inheritance refers to the concept of a class inheriting properties and methods from a single parent class. In PHP, a class can extend only one parent class using the `extends` keyword. The derived class, also known as the child class, inherits all the public and protected properties and methods of the parent class.
    <?php
    
    class ParentClass {
        // properties and methods
    }
    
    class ChildClass extends ParentClass {
        // properties and methods
    }
  2. Multiple inheritance (achieved through interfaces): Multiple inheritance refers to the concept of a class inheriting properties and methods from multiple parent classes. However, PHP does not support multiple inheritance directly between classes. Instead, PHP supports multiple inheritance through interfaces. An interface defines a contract that a class must adhere to by implementing its methods.
    <?php
    
    interface Interface1 {
        // methods
    }
    
    interface Interface2 {
        // methods
    }
    
    class ChildClass implements Interface1, Interface2 {
        // properties and methods
    }

    By implementing multiple interfaces, a class can inherit and implement the methods defined in those interfaces. This allows for achieving a form of multiple inheritance in PHP.
    It's important to note that PHP supports only single class inheritance but allows implementing multiple interfaces, providing a way to achieve similar functionality to multiple inheritance.

  3. Multi-level inheritance: Multi-level inheritance refers to the concept of a class inheriting properties and methods from a parent class, which itself has inherited from another parent class. In this type of inheritance, a class extends a class that has already extended another class.
    <?php
    
    class GrandParentClass {
        // properties and methods
    }
    
    class ParentClass extends GrandParentClass {
        // properties and methods
    }
    
    class ChildClass extends ParentClass {
        // properties and methods
    }
    

    In the above example, the ChildClass inherits properties and methods from both the ParentClass and the GrandParentClass. It forms a chain where the ChildClass indirectly inherits the properties and methods from the GrandParentClass through the ParentClass.

    Multi-level inheritance allows classes to inherit and extend the functionality of multiple classes in a hierarchical manner. The child class can access the properties and methods of both its immediate parent class and its ancestor classes.

    In PHP, a class can directly extend only one parent class. However, through multi-level inheritance, a class can indirectly inherit from multiple classes similar to multiple inheritance through interfaces.

Tags

x

Work

Therefore logo
80 Atlantic Ave, Toronto, ON Canada
Email: hello@therefore.ca
Call us: +1 4166405376
Linkedin

Let us know how we can help!