PHP interface multiple inheritance and tarits how to implement multiple inheritance effects tutorial details

Source: Internet
Author: User
Tags php class traits
In this paper, the methods of multiple inheritance and tarits implementation of PHP interface are described. Share to everyone for your reference, as follows:

Interface Multiple Inheritance

In object-oriented PHP, an interface can inherit an interface. A PHP class can inherit only one parent class (single inheritance), but interfaces can implement multiple inheritance and can inherit one or more interfaces. Of course, the inheritance of the interface is the same as the inheritance of the class using the extends keyword, to multiple inherited words as long as the inherited interface separated by commas.

It is important to note that when your interface inherits from other interfaces, it inherits the static constant properties and abstract methods of the parent interface directly, so the class must implement all the relevant abstract methods when implementing the interface.

The following examples illustrate:

1. Inherit a single interface

I

Nterface testa{function Echostr ();} Interface Testb extends testa{function dancing ($name);} class TESTC implements testb{function Echostr () {echo ' interface inheritance, to implement All relevant abstract methods! "; echo "";} function Dancing ($name) {echo $name. " are dancing! ";}} $demo =new TESTC (); $demo->echostr (); $demo->dancing ("model"); Run result/**   interface inheritance, to implement all relevant abstract methods The model is dancing!**/

2. Inherit multiple interfaces

Interface testa{function Echostr ();} interface testb{function dancing ($name);} interface Testc extends testa,testb{fu Nction singing ($nickname); } class TestD implements testc{function Echostr () {echo "interface inheritance, to implement all related methods of the parent interface!"; echo ";} function Dancing ($name) {echo $name. " are dancing! "; echo "";} function Singing ($nickname) {echo $nickname. " is Singing! ";}} $demo =new TestD (); $demo->echostr (); $demo->dancing ("model"); $demo->singing ("Jay Chou"); Run the result/** interface inheritance, to implement all related methods of the parent interface! The model is dancing! Jay Chou is singing! **/

Tarits Multiple Inheritance

Multiple inheritance in a class can inherit multiple parent classes at the same time, the ability to combine multiple parent classes C + + is the use of this model to enhance the flexibility of integration, but multiple inheritance is too flexible, and will bring "diamond inheritance", so the use of a lot of difficulties, the model becomes complex, Most languages now give up the model of multiple inheritance.

But some occasions want to use multiple inheritance, but PHP did not inherit much, so the invention of such a thing.

Traits can be understood as a set of methods that can be called by different classes, but traits is not a class! cannot be instantiated. The first example looks at the syntax:

Trait mytrait{function TraitMethod1 () {} function TraitMethod2 () {}}///Then call this traits, syntax: class myclass{use mytrait;}// This allows you to invoke the method in traits using the use Mytraits, for example: $obj = new MyClass (); $obj-TraitMethod1 (); $obj-TRAITMETHOD2 (); >

This article describes the PHP interface multiple inheritance and tarits implementation of multi-inheritance effect of the method, more relevant knowledge, please pay attention to the PHP Chinese network.

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.