inheritance in PHP Classes

How to implement inheritance in PHP Classes?

In this tutorial, you will learn how to implement the inheritance in PHP OOP Classes.

Must-KnowWhat is Inheritance?

How to inherit a class in PHP?

In PHP, you can implement the inheritance with the help of the extends keyword.

<?php
class ParentClass{
    // Properties and Methods
}

class ChildClass extends ParentClass{
    // Properties and Methods
}

Example of PHP inheritance

<?php
class ParentClass
{
    public $name = 'John';

    public function sayHello(){
        echo "Hello World!";
    }
}

class ChildClass extends ParentClass
{
    public $age = 21;

    public function showUser(){
        echo "Name - {$this->name}, Age - {$this->age}\n";
    }
}

$obj1 = new ChildClass();
$obj1->showUser();
$obj1->sayHello();
Name - John, Age - 21
Hello World!

How to call the Parent class constructor when you use inheritance in PHP?

You can face three situations –

  1. Only the parent class has the constructor function.
  2. Only the Child class has the constructor function.
  3. Both parent and child classes have the constructor functions.

1. Only the parent class has the constructor function

If the parent class has the constructor and the child class does not, then when you create an object of the child class It will automatically invoke the Parent class constructor.

<?php
class ParentClass
{
    function __construct()
    {
        echo "Hi from Parent Class Constructor.";
    }
}

class ChildClass extends ParentClass
{
    public $name = "John";
}

$obj1 = new ChildClass();
Hi from Parent Class Constructor.

2. Only the Child class has the constructor function

In this case, the child class constructor will run.

<?php
class ParentClass
{
    public $name = "John";
}

class ChildClass extends ParentClass
{
    function __construct()
    {
        echo "Hi from Child Class Constructor.";
    }
}

$obj1 = new ChildClass();
Hi from Child Class Constructor.

3. Both parent and child classes have the constructor functions

If Both classes have the constructor, then the child class constructor will run when you create an instance of the child class.

<?php
class ParentClass
{
    function __construct()
    {
        echo "Hi from Parent Class Constructor.";
    }
}

class ChildClass extends ParentClass
{
    function __construct()
    {
        echo "Hi from Child Class Constructor.";
    }
}

$obj1 = new ChildClass();
Hi from Child Class Constructor.

How to call the parent class constructor when the child class also has a constructor?

First way

Remove the constructor of the child class.

Second way

In PHP, there is a keyword called parent::, you can use it to call the parent class constructor.

parent::__construct();
<?php
class ParentClass
{
    function __construct()
    {
        echo "Hi from Parent Class Constructor.\n";
    }
}

class ChildClass extends ParentClass
{
    function __construct()
    {
        parent::__construct();
        echo "Hi from Child Class Constructor.";
    }
}

$obj1 = new ChildClass();
Hi from Parent Class Constructor.
Hi from Child Class Constructor.

How to pass arguments to the parent class constructor when the child class also has a constructor?

In PHP, through the child class constructor, you can pass arguments to the parent class constructor.

<?php
class ParentClass
{
    function __construct($name)
    {
        echo "Hi $name.\n";
    }
}

class ChildClass extends ParentClass
{
    function __construct($a)
    {
        parent::__construct($a);
        echo "Hi from Child Class Constructor.";
    }
}

$obj1 = new ChildClass('Mark');
Hi Mark.
Hi from Child Class Constructor.

How to call the grandparent class constructor in PHP?

Grand Parent Class 🤔??? – Yes, PHP also supports Multilevel inheritance, and it supports all types of inheritance except the Multiple inheritance.

To call the GrandParent class constructor, you have to call the parent::__construct() in the child class constructor as well as the parent class constructor.

<?php
class GrandParent{
    function __construct()
    {
        echo "Hi from GrandParent Class Constructor.\n";
    }
}

class ParentClass extends GrandParent
{
    function __construct()
    {
        parent::__construct();
        echo "Hi from Parent Class Constructor.\n";
    }
}

class ChildClass extends ParentClass
{
    function __construct()
    {
        parent::__construct();
        echo "Hi from Child Class Constructor.";
    }
}

$obj1 = new ChildClass();
Hi from GrandParent Class Constructor.
Hi from Parent Class Constructor.
Hi from Child Class Constructor.

How to call the grandparent class constructor directly from the child class?

You can directly call the GrandParent class constructor through the class name like – ClassName::__construct(), in this case GrandParent::__construct().

<?php
class GrandParent{
    function __construct()
    {
        echo "Hi from GrandParent Class Constructor.\n";
    }
}

class ParentClass extends GrandParent
{
    function __construct()
    {
        echo "Hi from Parent Class Constructor.\n";
    }
}

class ChildClass extends ParentClass
{
    function __construct()
    {
        GrandParent::__construct();
        parent::__construct();
        echo "Hi from Child Class Constructor.";
    }
}

$obj1 = new ChildClass();
Hi from GrandParent Class Constructor.
Hi from Parent Class Constructor.     
Hi from Child Class Constructor.