New feature series of modern PHP (III)-Trait overview Trait is a new concept introduced by PHP 5.4. it looks like both class and interface, but not actually. Trait can be seen as part of the class implementation, it can be mixed into one or more existing PHP classes, which have two functions: indicate what the class can do and provide modular implementation. Trait is a code reuse technology that provides a flexible code reuse mechanism for PHP's single inheritance restrictions.
Why use Trait?
PHP uses a typical single inheritance model. In this model, we first compile a common root class to implement basic functions, and then expand this root class, create more specific child classes and inherit them from the parent class. This is called an inheritance hierarchy, which is used by many programming languages. Most of the time, this typical inheritance model works well, but what should we do if we want two unrelated PHP classes to have similar behavior?
Trait was born to solve this problem. Trait can inject a modular implementation method into multiple irrelevant classes to improve code reuse. it complies with the DRY (Don't Repeat Yourself) principle. For example, Laravel uses Trait to implement underlying user authentication logic and soft deletion. Take Laravel's built-in AuthController as an example. the number of logon attempts, registration attempts, and logon failure attempts is achieved through Trait:
How to create a Trait
Creating a Trait is very simple. it is similar to creating a class, except that the keyword is trait rather than class. the preceding ThrottlesLogin is used as an example:
We declare and define a trait through Trait, and then we can define the attributes and methods to be used like a class in this Trait.
In addition, Trait supports nesting and combination, that is, combining one or more Trait (multiple with and separated) into one Trait. for example, AuthenticatesAndRegistersUsers is like this:
Using multiple Trait may cause name conflicts. the above code provides a solution: Use the insteadof keyword. if the redirectPath and getGuard methods are defined in AuthenticatesUsers and RegistersUsers, the corresponding method instead of RegistersUsers will be obtained from AuthenticatesUsers. You can also use the as keyword to create an alias for the method to avoid name conflicts.
In addition, the abstract method and static method can be defined in Trait. the abstract method must be implemented in its class.
Here, we also need to declare the priority of the call method: call class> Trait> parent class (if any), the method can be overwritten, but the attribute is not good, if a property is defined in Trait, an error is returned if the property is also defined in the call class.
How to use Trait
The usage of Trait is also very simple, and the above shows clearly that the use keyword is used.
You may have noticed that both the namespace and Trait use the use keyword. The difference lies in the import location. the namespace is imported in the definition of the class in vitro, while the Trait is imported in the definition of the class.
Note: The PHP interpreter will copy Trait to the class definition body during compilation, but it will not handle incompatibility issues introduced by this operation. If Trait assumes that the class has specific attributes or methods, make sure that the class does have the corresponding attributes or methods.