Current Location: Home> Latest Articles> PHP Interface Inheritance Explained: Definition, Usage, and Example Analysis

PHP Interface Inheritance Explained: Definition, Usage, and Example Analysis

gitbox 2025-06-17

1. What is Interface Inheritance?

In PHP object-oriented programming, interface inheritance allows one interface to inherit all methods and constants from another interface, and new methods can be added in the inherited interface. Interface inheritance promotes code reuse, reduces duplication, and improves maintainability.

2. Definition and Usage of Interface Inheritance

2.1 Defining Interface Inheritance

Interface inheritance is achieved using the "extends" keyword. By using the "extends" keyword in an interface declaration followed by the interface name to be inherited, the inheritance is established.


interface InterfaceA {
    public function methodA();
}
<p>interface InterfaceB extends InterfaceA {<br>
public function methodB();<br>
}<br>

In the example above, InterfaceB inherits from InterfaceA and adds a new method, methodB().

2.2 Usage of Interface Inheritance

Interface inheritance allows a class to implement multiple interfaces. Methods and constants inherited from parent interfaces can be used directly in the child interfaces or classes.


interface InterfaceA {
    const CONSTANT_A = "A";
    public function methodA();
}
<p>interface InterfaceB extends InterfaceA {<br>
const CONSTANT_B = "B";<br>
public function methodB();<br>
}</p>
<p>class MyClass implements InterfaceB {<br>
public function methodA() {<br>
// Implementation of method from InterfaceA<br>
}</p>
    // Implementation of method from InterfaceB
}

}

In this example, the MyClass class implements the InterfaceB interface and satisfies the required methods, methodA() and methodB(). Additionally, since InterfaceB inherits from InterfaceA, MyClass can directly use the constants CONSTANT_A and CONSTANT_B.

3. Example Analysis of Interface Inheritance

The following example will provide a detailed explanation of interface inheritance definition and usage.

3.1 Defining the Parent Interface


interface Shape {
    const COLOR = "red";
    public function draw();
}

In this example, we define a Shape interface, which includes a constant COLOR and an abstract method draw().

3.2 Defining the Child Interface


interface Circle extends Shape {
    public function calculateArea();
}

In this example, the Circle interface extends the Shape interface and adds a new method, calculateArea().

3.3 Implementing the Interface


class CircleShape implements Circle {
    public function draw() {
        echo "Drawing a circle";
    }
    // Calculate the area of the circle
}

}

In this example, we create a CircleShape class to implement the Circle interface. By implementing the draw() and calculateArea() methods, CircleShape can now draw a circle and calculate its area.

From the examples above, we can see that interface inheritance not only allows for the reuse of interfaces but also extends the functionality of interfaces, improving code maintainability and readability.

4. Conclusion

This article has thoroughly explained the definition and usage of interface inheritance in PHP object-oriented programming. By inheriting interfaces, we can achieve interface reuse and extension, enhancing code maintainability and readability. Through the examples, you can learn how to define parent interfaces, child interfaces, and classes that implement interfaces. In actual development, effectively using interface inheritance can improve coding efficiency, reduce code coupling, and increase code reusability.