"Go" php Trait features, phptrait features _php tutorials

Source: Internet
Author: User
Tags modifiers

"Go" php Trait features, phptrait features


Trait is added in PHP5.4, which is neither an interface nor a class. The main purpose is to solve the limitations of single-inheritance language. is a solution to PHP multiple inheritance. For example, it would be cumbersome to inherit two Abstract classes at the same time, Trait to solve the problem. It can be added to one or more existing classes. It declares what the class can do (indicating its interface characteristics), and also contains a concrete implementation (indicating its class characteristics)

Simple to use

First of all, of course declaring a trait,php5.4 adds the Trait keyword

1 trait first_trait {2      function /*  * / }3      function/** / }  4 }

Also, if you want to use the Trait in Class, use the Using keyword

1 class First_class {  2     // Note this line, declaring the use of first_trait 3      Use first_trait; 4 }  5  6$objnew  first_class (); 7 8 // executing the method from trait 9 $obj // valid Ten $obj // valid

Using multiple Trait

Multiple Trait can be used in the same Class

1 Trait first_trait2 {3     functionFirst_method () {Echo"Method1"; }4 }5 6 trait second_trait {7     functionSecond_method () {Echo"Method2"; }8 }9 Ten classFirst_class { One     //Now using the more than one trait A      UseFirst_trait,second_trait; - } -  the $obj=NewFirst_class (); -  - //Valid - $obj->first_method ();//print:method1 +  - //Valid + $obj->second_method ();//print:method2

Nesting between Trait

At the same time, Trait can also be nested between each other, for example

1 trait first_trait {2     functionFirst_method () {Echo"Method1"; }3 }4 5 trait second_trait {6      Usefirst_trait;7     functionSecond_method () {Echo"Method2"; }8 }9 Ten classFirst_class { One     //Now using A      Usesecond_trait; - } -  the $obj=NewFirst_class (); -  - //Valid - $obj->first_method ();//print:method1 +  - //Valid + $obj->second_method ();//print:method2

Abstract method of Trait

We can declare in Trait the abstract method that needs to be implemented, so that the Class that uses it must implement it

1 trait first_trait {  2     function Echo "Method1";} 3 4     // you can add modifiers here, stating that the calling class must implement it 5     Abstract  Public function Second_method (); 6 }  7  8class  first_method {  9      use first_trait; Ten  One     function Second_method () {         /*  * *    +}

Trait conflict

The simultaneous use of multiple Trait will inevitably conflict, which requires us to solve. PHP5.4 brings the relevant keyword syntax from the syntax:insteadof and as, use see

1 trait first_trait {2     functionfirst_function () {3         Echo"From First Trait";4     }5 }6 7 trait second_trait {8     //The name here is the same as the first_trait, there will be conflicts9     functionfirst_function () {Ten         Echo"From Second Trait"; One     } A } -  - classFirst_class { the      UseFirst_trait,second_trait { -         //declare here to replace First_function with first_trait - //Second_trait stated in the -First_trait::first_function insteadof second_trait; +     } - }   +  A $obj=NewFirst_class (); at  - //Output:from First Trait - $obj->first_function ();

Above is a few Trait more basic use, more detailed can refer to the official manual. Here is a summary of the points to note:

    • Trait overrides the parent class method that invokes the class inheritance
    • Trait cannot use new instantiation as Class
    • A single Trait can be made up of multiple Trait
    • In a single Class, you can use multiple Trait
    • Trait supports modifiers (modifiers), such as final, static, abstract
    • We can use the insteadof and as operators to resolve conflicts between Trait

Article Source: http://www.kuqin.com/web/20111119/315048.html

http://www.bkjia.com/PHPjc/1115250.html www.bkjia.com true http://www.bkjia.com/PHPjc/1115250.html techarticle The trait feature of "Go" php, phptrait feature trait is added in PHP5.4, it is neither an interface nor a class. The main purpose is to solve the limitations of single-inheritance language. PHP multiple inheritance is a ...

  • 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.