Home >Backend Development >PHP Tutorial >A way to achieve code reuse in PHP Traits New Features_PHP Tutorial
This article mainly introduces a method for PHP to implement code reuse. Information about the new features of traits is required. Friends can refer to it
I came across traits while reading the yii2 source code, so I studied it and wrote a blog to record it.
Since PHP 5.4.0, PHP implements a method of code reuse called traits.
Traits is a code reuse mechanism for single-inheritance languages like PHP. Traits are designed to reduce the constraints of single-inheritance languages and allow developers to freely reuse method sets in independent classes within different hierarchies. The semantics of traits and class composition define a way to reduce complexity and avoid the typical problems associated with traditional multiple inheritance and mixins.
Trait is similar to a class, but is only designed to combine functionality in a fine-grained and consistent way. Trait cannot be instantiated by itself. It adds a combination of horizontal features to traditional inheritance; that is, members of application classes do not need to be inherited.
Trait example
The code is as follows:
Priority
Members inherited from the base class are overridden by members inserted by the trait. The order of precedence is that members from the current class override the trait's methods, and the trait overrides the inherited methods.
Example of priority
The code is as follows:
The above routine will output: Hello World!
Members inherited from the base class are overridden by the sayHello method in the inserted SayWorld Trait. Its behavior is consistent with the methods defined in the MyHelloWorld class. The order of precedence is that methods in the current class override trait methods, which in turn override methods in the base class.
Another example of priority order
The code is as follows:
The above routine will output: Hello Universe!
Multiple traits
Separated by commas, list multiple traits in the use statement, which can all be inserted into a class.
Examples of usage of multiple traits
The code is as follows:
The above routine will output: Hello World!
Conflict resolution
If two traits insert a method with the same name, a fatal error will occur if the conflict is not explicitly resolved.
In order to resolve the naming conflict of multiple traits in the same class, you need to use the insteadof operator to explicitly specify which of the conflicting methods to use.
The above method only allows to exclude other methods. The as operator can introduce one of the conflicting methods under another name.
Examples of conflict resolution
The code is as follows:
In this example Talker uses traits A and B. Since A and B have conflicting methods, they define using smallTalk from trait B and bigTalk from trait A.
Aliased_Talker uses the as operator to define talk as an alias of B's bigTalk.
Modify method access control
Using as syntax can also be used to adjust method access control.
Example of modifying method access control
The code is as follows:
Compose trait from trait
Just as classes can use traits, other traits can also use traits. By using one or more traits when a trait is defined, it can combine some or all members of other traits.
Examples of composing traits from traits
The code is as follows:
The above routine will output: Hello World!
Abstract member of Trait
In order to enforce requirements on the classes used, traits support the use of abstract methods.
Indicates an example of enforcing requirements through abstract methods
The code is as follows:
Static members of Trait
Traits can be defined by static members and static methods.
Example of static variable
The code is as follows:
Example of static method
The code is as follows:
Examples of static variables and static methods
The code is as follows:
Properties
Traits can also define properties.
Example of defining attributes
The code is as follows:
If the trait defines a property, the class cannot define a property with the same name, otherwise an error will be generated. If the property's definition in the class is compatible with its definition in the trait (same visibility and initial value) then the error level is E_STRICT, otherwise it is a fatal error.
Examples of conflicts
The code is as follows:
Differences in Use
Examples of different uses
The code is as follows:
The first use is use FooTest for namespace, and FooTest is found. The second use is to use a trait, and FooBarFooTest is found.
__CLASS__ and __TRAIT__
__CLASS__ returns the class name of the use trait, __TRAIT__ returns the trait name
An example is as follows
The code is as follows:
Trait singleton
Examples are as follows
The code is as follows:
Call trait method
Although it is not obvious, if the Trait method can be defined as a static method in a normal class, it can be called
Examples are as follows
The code is as follows:
Are you familiar with the new features of traits? I hope this article can be helpful to you.