Home »
PHP »
PHP Programs
PHP program to demonstrate the single inheritance
Here, we are going to demonstrate the single inheritance in PHP.
By Nidhi Last updated : December 19, 2023
PHP - Single Inheritance Example
Here, we will implement single inheritance. In the single inheritance, we will inherit the one base class into a single derived class. We will use the extends keyword to implement inheritance.
Example of single inheritance in PHP
The source code to demonstrate the single inheritance is given below. The given program is compiled and executed successfully.
<?php
//PHP program to demonstrate the single inheritance.
class Base
{
function BaseFun()
{
echo "BaseFun() called<br>";
}
}
class Derived extends Base
{
function DerivedFun()
{
echo "DerivedFun() called<br>";
}
}
$dObj = new Derived();
$dObj->BaseFun();
$dObj->DerivedFun();
?>
Output
BaseFun() called
DerivedFun() called
Explanation
In the above program, we created two classes Base and Derived. Here, we inherited the Base class into the derived class using the extends keyword.
The Base class contains a function BaseFun(), and Derived class also contains only one function DerivedFun().
At last, we created the object $dObj of the derived class. Then called BaseFun() and DerivedFun() using the object that will print the appropriate message on the webpage.
In the above example, we have used the following PHP topics:
PHP Class & Object Programs »