This article introduces to you about the characteristics of PHP trait and usage introduction (with code), there is a certain reference value, the need for friends can refer to, I hope to help you.
Since PHP5.4.0, PHP has implemented a method of code reuse, called traits.
Traits 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 the set of methods in separate classes within different hierarchies. The semantics of Traits and class combinations are defined as a way to reduce complexity and avoid typical problems associated with traditional multi-inheritance and mixed-Class (Mixin).
Trait is similar to a class, but only designed to combine functionality in a fine-grained and consistent way. Trait cannot be instantiated by itself. It adds a combination of horizontal attributes to traditional inheritance, meaning that members of the application class do not need inheritance.
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
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 (); Nested between print:methodtrait, Trait can also be nested with each other, such as 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 are a few points to note:
Trait overrides the parent class method that invokes the class inheritance
Trait cannot instantiate
single Trait as class with multiple Trait
in a single class, you can use multiple trait< Br>trait supports modifiers (modifiers), such as final, static, abstract
We can use insteadof and as operators to resolve conflicts between Trait