Since PHP 5.4.0, PHP has implemented a code reuse method called trait.
Trait is a code reuse mechanism that is prepared for PHP-like single-inheritance languages. Trait to reduce the limitations of single-inheritance languages, developers are free to reuse method in separate classes within different hierarchies. The semantics of Trait and class combinations define a way to reduce complexity, avoiding traditional multi-inheritance and Mixin class-related typical problems.
Trait is similar to Class, but only designed to combine functionality in a fine-grained and consistent way. cannot be instantiated by trait itself. It adds a combination of horizontal features to the traditional inheritance, meaning that no inheritance is required between several classes of the application.
Trait Example
First of all, of course declaring a trait,php5.4 adds the Trait keyword
Trait first_trait {function First_method () {/* code here */}function Second_method () {/* code here/}}
Also, if you want to use the Trait in Class, use the Using keyword
Class First_class {//Note this line, declaring using First_traituse first_trait;} $obj = new First_class ();//Executing the method from Trait$obj->first_method (); Valid$obj->second_method (); Valid
Using multiple Trait
Multiple Trait can be used in the same Class
Trait First_trait{function First_method () {echo "Method";}} Trait second_trait {function Second_method () {echo "Method";}} Class First_class {//now using more than one traituse first_trait, second_trait;} $obj = new First_class ();//Valid$obj->first_method (); print:method//Valid$obj->second_method (); Print:method
Nesting between Trait
At the same time, Trait can also be nested between each other, for example
Trait first_trait {function First_method () {echo "Method";}} Trait second_trait {use first_trait;function Second_method () {echo "Method";}} Class First_class {//now using with use second_trait;} $obj = new First_class ();//Valid$obj->first_method (); print:method//Valid$obj->second_method (); Print:method
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
Trait first_trait {function First_method () {echo "method";} The modifier can be added here, stating that the calling class must implement its abstract public function Second_method ();} Class First_method {use First_trait;function Second_method () {/* 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
Trait first_trait {function first_function () {echo "From First trait";}} Trait second_trait {//The name here is the same as first_trait, there will be conflict function first_function () {echo "from second trait";}} Class First_class {Use First_trait, second_trait {//declared here using first_trait first_function replacement//second_trait T::first_function insteadof second_trait;}} $obj = new First_class ();//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