Home »
PHP »
PHP programs
PHP program to inherit an abstract class into multiple non-abstract classes
Here, we are going to learn how to inherit an abstract class into multiple non-abstract classes in PHP?
Submitted by Nidhi, on November 21, 2020 [Last updated : March 13, 2023]
Inheriting an Abstract Class into Multiple Non-Abstract Classes
Here, we will create an abstract class that contains the abstract functions and then we will inherit the abstract class into multiple non-abstract classes and define the abstract function in all non-abstract classes.
Program
The source code to inherit an abstract class into multiple non-abstract classes is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
<?php
//PHP program to inherit an abstract class into
//multiple non-abstract classes.
abstract class AbsClass
{
public abstract function fun();
}
class Sample1 extends AbsClass
{
public function fun()
{
printf("Sample1::fun() called<br>");
}
}
class Sample2 extends AbsClass
{
public function fun()
{
printf("Sample2::fun() called<br>");
}
}
class Sample3 extends AbsClass
{
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 abstract class AbsClass that contains abstract functions fun().
After that, we created three non-abstract classes Sample1, Sample2, and Sample3. Here, we used the extends keyword to inherit an abstract class in the non-abstract class. Then we inherited the same abstract class into all non-abstract classes.
At last, we created three objects $obj1, $obj2, and $obj3, and called functions that will print the appropriate message on the webpage.
PHP Class & Object Programs »