PHP interface inheritance and multi-interface inheritance principles and implementation methods
This article describes the principles and implementation methods of PHP interface inheritance and multi-interface inheritance. We will share this with you for your reference. The details are as follows:
In PHP interfaces, interfaces can inherit interfaces. Although the PHP class can only inherit one parent class (single inheritance), but different interfaces and classes, interfaces can implement multiple inheritance, can inherit one or more interfaces. Of course, the inheritance of interfaces also uses the extends keyword. To Inherit multiple interfaces, you only need to use commas to separate the inherited interfaces.
Note that when your interface inherits other interfaces, it directly inherits the static constant attributes and abstract methods of the parent interface. Therefore, you must implement all the relevant abstract methods when implementing the class interface.
Now you know something about the inheritance of PHP interfaces. The following example is for your reference. The Code is as follows:
<? Phpinterface father {function shuchu ();} interface fam extends father {function cook ($ name);} class test implements fam {function shuchu () {echo "interface inheritance, two abstract methods are required: "; echo" <br> ";} function cook ($ name) {echo" the people who often cook are :". $ name ;}$ t = new test (); $ t-> shuchu (); $ t-> cook ("mom");?>
The code execution result is as follows:
Interface inheritance: two abstract methods are usually used for cooking: Mom
The preceding example shows that the interface inherits an interface. Therefore, when the test class implements the fam interface, two abstract methods are required, that is, the subclass of the interface and the abstract method of the parent class are both instance.
The following is an example of multi-inheritance of an interface. The Code is as follows:
<? Phpinterface father {function shuchu ();} interface mother {function dayin ($ my);} interface fam extends father, mother {function cook ($ name );} class test implements fam {function dayin ($ my) {echo "my name is :". $ my; echo "<br>" ;}function shuchu () {echo "interface inheritance, two abstract methods must be implemented"; echo "<br> ";} function cook ($ name) {echo "people who often cook at ordinary times are :". $ name ;}$ t = new test (); $ t-> shuchu (); $ t-> dayin ("Xiaoqiang "); $ t-> cook ("mom");?>
Sample running result:
Interface inheritance: two abstract methods should be implemented. My name is: Mom, who often cooks in Xiaoqiang.
Because the interface inherits two interfaces, all the abstract methods of these three abstract classes must be used in all instances. There are three abstract methods in total. After reading these two examples, you should be familiar with interface inheritance. In fact, it is a single inheritance and multi-inheritance, as long as all the relevant abstract methods are implemented.