PHP Tutorial

PHP Examples

PHP Practice

PHP program to implement multiple-inheritance using the interface

Here, we are going to learn how to implement multiple-inheritance using the interface in PHP?
Submitted by Nidhi, on November 20, 2020 [Last updated : March 13, 2023]

Multiple-Inheritance Using Interface

Here, we will implement multiple-inheritance by inheriting a class and an interface into the derived class.

PHP code to implement multiple-inheritance using the interface

The source code to implement multiple-inheritance using the interface is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

<?php
//PHP program to implement multiple-inheritance 
//using the interface.
class Base
{
    public function Fun1()
    {
        printf("Fun1() called<br>");
    }
}

interface Inf
{
    public function Fun2();
}

class Derived extends Base implements Inf
{
    function Fun2()
    {
        printf("Fun2() called<br>");
    }

    function Fun3()
    {
        printf("Fun3() called<br>");
    }
}

$obj = new Derived();

$obj->Fun1();
$obj->Fun2();
$obj->Fun3();

?>

Output

Fun1() called
Fun2() called
Fun3() called

Explanation

In the above program, we created a class Base and an interface Inf and inherited the Base class and Inf interface into the Derived class.

The Base class contains the function Fun1(), and interface Inf contains the declaration of Fun2(). We defined the Fun2() and Fun3() inside the Derived class.

At last, we created an object $obj of Derived class and called functions that will print the appropriate message on the webpage.

PHP Class & Object Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.