Q:

PHP program to demonstrate the single inheritance

belongs to collection: PHP Classes & Objects Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Classes & Objects Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
PHP program to call a base class constructor from ... >>
<< PHP program to demonstrate the use of variable arg...