Google News
logo
PHP program to implement multiple-inheritance using the interface
PHP does not support multiple inheritance, which means a class can only inherit from one parent class. However, we can use interfaces to achieve a similar effect. Here is an example program that demonstrates how to implement multiple interfaces in the same class :
Program :
<?php
interface Shape {
    public function getArea();
}

interface Color {
    public function getColor();
}

class Circle implements Shape, Color {
    private $radius;
    private $color;

    public function __construct($radius, $color) {
        $this->radius = $radius;
        $this->color = $color;
    }

    public function getArea() {
        return pi() * pow($this->radius, 2);
    }

    public function getColor() {
        return $this->color;
    }
}

$circle = new Circle(5, "red");
echo "Area of circle is: " . $circle->getArea() . "\n";
echo "Color of circle is: " . $circle->getColor();
?>
In this program, we define two interfaces `Shape` and `Color`, and a class `Circle` that implements both of these interfaces. The `Circle` class has two private attributes `$radius` and `$color`, and two methods `getArea()` and `getColor()` that implement the methods defined in the `Shape` and `Color` interfaces, respectively.

We then create an object of the `Circle` class and call its `getArea()` and `getColor()` methods to get the area and color of the circle, respectively. The output of the program will be :
Output :
Area of circle is: 78.539816339745
Color of circle is: red