PHP interface inheritance and multi-interface inheritance principles and implementation methods

Source: Internet
Author: User
Tags fam

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.