Google News
logo
PHP program to inherit an abstract class into multiple non-abstract classes
In PHP, an abstract class is a class that cannot be instantiated, but can be extended. It can have both abstract and non-abstract methods, and if a class extends an abstract class, it must implement all the abstract methods. Here's an example of how to inherit an abstract class into multiple non-abstract classes :
Program :
<?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
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.