PHP program to implement an interface into multiple classes

Here, we are going to learn how to implement an interface into multiple classes in PHP?
Submitted by Nidhi, on November 20, 2020

Here, we will create an interface that contains the declaration of functions and then we will implement the interface into multiple classes with function definitions.

Program/Source Code:

The source code to implement an interface into multiple classes is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

<?php
//PHP program to implement an interface 
//into multiple classes.
interface Inf
{
    public function fun();
}

class Sample1 implements Inf
{
    public function fun()
    {
        printf("Sample1::fun() called<br>");
    }
}

class Sample2 implements Inf
{
    public function fun()
    {
        printf("Sample2::fun() called<br>");
    }
}

class Sample3 implements Inf
{
    public function fun()
    {
        printf("Sample3::fun() called<br>");
    }
}

$obj1 = new Sample1();
$obj2 = new Sample2();
$obj3 = new Sample3();

$obj1->fun();
$obj2->fun();
$obj3->fun();
?>

Output:

Sample1::fun() called
Sample2::fun() called
Sample3::fun() called

Explanation:

In the above program, we created an interface Inf that contains the declaration of function fun().

After that, we created three classes Sample1, Sample2, and Sample3. Here, we used the implements keyword to implement an interface into the class.  Then we implemented the same interface in all three classes.

At last, we created three objects $obj1, $obj2, and $obj3, 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.