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

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

Program/Source Code:

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.