PHP program to demonstrate the multi-level inheritance

Here, we are going to demonstrate the multi-level inheritance in PHP.
Submitted by Nidhi, on November 20, 2020 [Last updated : March 13, 2023]

Multi-Level Inheritance Example

Here, we will implement multi-level 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.

PHP code to implement multi-level inheritance

The source code to demonstrate the multi-level inheritance is given below. The given program is compiled and executed successfully.

//PHP program to demonstrate the multi-level inheritance.
class Base
    function BaseFun()
        echo "BaseFun() called<br>";

class Derived1 extends Base
    function Derived1Fun()
        echo "Derived1Fun() called<br>";

class Derived2 extends Derived1
    function Derived2Fun()
        echo "Derived2Fun() called<br>";

$dObj = new Derived2();




BaseFun() called
Derived1Fun() called
Derived2Fun() called


In the above program, we created three classes Base, Derived1, and Derived2. Here, we inherited the Base class into Derived1 class and then inherit the Derived1 class into Derived2 class using the extends keyword.

At last, we created the object $dObj of the Derived2 class. Then called the functions of all classes using the object of Derived2 class that will print appropriate messages on the webpage.

PHP Class & Object Programs »

Related Programs

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.