Home »
PHP »
PHP programs
PHP program to demonstrate the inheritance of interfaces
Here, we are going to demonstrate the inheritance of interfaces in PHP.
Submitted by Nidhi, on November 20, 2020 [Last updated : March 13, 2023]
Inheritance of Interfaces
Here, we will inherit an interface into another interface using the extends keyword and then implement the derived interface into the class.
PHP code to implement inheritance of interfaces
The source code to demonstrate the inheritance of interfaces is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
<?php
//PHP program to demonstrate the inheritance of interfaces.
interface Inf1
{
public function Fun1();
}
interface Inf2 extends Inf1
{
public function Fun2();
}
class Sample implements Inf2
{
function Fun1()
{
printf("Fun1() called<br>");
}
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, Inf2, and a class Sample. The Inf1 interface contains the declaration of Fun1(), and the Inf2 interface contains the declaration of Fun2().
Here, we inherited the Inf1 interface into Inf2 and then implemented the Inf2 interface into Sample class.
At last, we created an object $obj of Sample class and called functions that will print the appropriate message on the webpage.
PHP Class & Object Programs »