PHP interface Multi-Inheritance and tarits method to realize multi-inheritance effect _php tips

Source: Internet
Author: User
Tags php class traits
This article mainly introduces the PHP interface multiple inheritance and tarits implementation of multi-inheritance effect of the method, combined with an example of PHP interface-based multi-inheritance and PHP5.4 introduced in the tarits implementation of the multi-inheritance function simple operation skills, the need for friends can refer to the next

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

<?phpinterface 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 "<br>";  }  function Dancing ($name) {    echo $name. " I'm 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

<?phpinterface testa{  function Echostr ();} Interface testb{  function dancing ($name);} Interface TESTC extends testa,testb{  function singing ($nickname);} Class TestD implements testc{  function Echostr () {    echo "interface inheritance, to implement all related methods of the parent interface! ";    echo "<br/>";  }  function Dancing ($name) {    echo $name. " I'm dancing! ";    echo "<br/>";  }  function Singing ($nickname) {    echo $nickname. " I'm singing! ";  }} $demo =new TestD (), $demo->echostr (), $demo->dancing ("model"), $demo->singing ("Jay Chou");//Run result/**  interface inheritance, To implement all the 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:

<?phptrait mytrait{  function TraitMethod1 () {}  function traitMethod2 () {}}//Then call this traits, the syntax is: 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 (); >

Specific introduction and use can look at the official introduction.

Articles you may be interested in:

A detailed description of the cache () usage of THINKPHP5 Framework database coherent operation

thinkPHP3.2 How to implement pagination custom styles

Example analysis of multi-image upload function implemented by Laravel framework +blob

Related Article

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.