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 classFirst_class {2 //Note this line, declaring the use of first_trait3 Usefirst_trait;4 }5 6 $obj=NewFirst_class ();7 8 //executing the method from trait9 $obj->first_method ();//validTen $obj->second_method ();//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 functionFirst_method () {Echo"Method1"; }3 4 //you can add modifiers here, stating that the calling class must implement it5 Abstract Public functionSecond_method ();6 }7 8 classFirst_method {9 Usefirst_trait;Ten One functionSecond_method () { A /*Code here*/ - } -}
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
Trait features of "Go" php