Home »
        PHP »
        PHP programs
    
    PHP program to inherit an abstract class and an interface in a non-abstract class
    
    
    
    
	    Here, we are going to learn how to inherit an abstract class and an interface in a non-abstract class in PHP?
	    
		    Submitted by Nidhi, on November 21, 2020 [Last updated : March 13, 2023]
	    
    
    
    Inheriting an Abstract Class and An Interface in a Non-Abstract Class
    Here, we will create an abstract class and an interface then we inherited them into a non-abstract class.
    Program
    The source code to inherit an abstract class and an interface in a non-abstract class is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
<?php
//PHP program to inherit an abstract class 
//and an interface in a non-abstract class.
abstract class Abs
{
    public abstract function fun1();
}
interface Inf
{
    public function fun2();
}
class Sample extends Abs implements Inf
{
    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 an abstract class Abs and an interface Inf and then we inherited the abstract class and interface into a non-abstract class Sample, and defined the functions.
    At last, we created three objects $obj of Sample class and called functions that will print the appropriate message on the webpage.
    PHP Class & Object Programs »
	
    
    
    
    
    
    
    
  
    Advertisement
    
    
    
  
  
    Advertisement