PHP program to implement multiple interfaces in the same class

Here, we are going to learn how to implement multiple interfaces in the same class in PHP?
Submitted by Nidhi, on November 20, 2020

Here, we will create two interfaces that contain the declaration of functions and then we will implement the interfaces in the same class.

Program/Source Code:

The source code to implement two interfaces in the same class is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

<?php
//PHP program to implement two interfaces in the same class.
interface Inf1
{
    public function fun1();
}

interface Inf2
{
    public function fun2();
}

class Sample implements Inf1, Inf2
{
    public function fun1()
    {
        printf("fun1() called<br>");
    }
    public function fun2()
    {
        printf("fun2() called<br>");
    }
}

$obj = new Sample();

$obj->fun1();
$obj->fun2();

?>

Output:

fun1() called
fun2() called

Explanation:

In the above program, we created two interfaces Inf1 and Inf2. The Inf1 interface contains the declaration of fun1() and the Inf2 interface contains the declaration of fun2().

After that, we created a class Sample. Here, we used the implements keyword to implement an interface into the class. Then we implemented both interfaces in the Sample class.

At last, we created an object $obj of Sample 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.