Why use Trait
The PHP language uses a typical single inheritance model, in which we first write a generic root class, implement basic functionality, then extend the root class, create more specific subclasses, and inherit the implementation directly from the parent class. This is called an inheritance hierarchy, and many programming languages use this pattern. Most of the time this typical inheritance model works well, but what if you want two unrelated PHP classes to behave like this?
Trait was born to solve this problem. Trait can inject a modular implementation into multiple unrelated classes to improve code reuse and conform to the dry (Don T Repeat yourself) principle. For example Laravel bottom user authentication related logic as well as the soft deletion realization and so on place all uses the trait to realize. Take Laravel AuthController
as an example, where the number of logins, registrations, and logon failure attempts are implemented through trait:
How to create a trait
Creating a trait is very simple, similar to creating a class, except that the keyword is used instead of the trait
class
above ThrottlesLogin
example:
We
trait
define a trait by declaration, and then we can define the properties and methods to use in this trait like a class.
In addition, trait supports nesting and grouping, which combines one or more trait (multiple, delimited) into a trait, such as AuthenticatesAndRegistersUsers
this:
Using multiple trait may cause a naming conflict problem, and the above code gives the solution: using
insteadof
keywords, if and
AuthenticatesUsers
RegistersUsers
in both definitions
redirectPath
and
getGuard
methods, you will
AuthenticatesUsers
get the corresponding method instead of
RegistersUsers
. You can also use
as
keywords to make individual names for methods, so you can avoid naming conflicts.
In addition, it may not be fully listed here, and trait also supports the definition of abstract methods and static methods, where the abstract method must be implemented in the class that uses it.
One thing to declare here is the precedence of the calling method: the invocation class >Trait> parent class (if any), the method can overwrite, but the property is not, if a property is defined in Trait, it is an error if the property is defined in the calling class.
How to use trait
Trait is also very simple to use, which has been shown very clearly, that is, the use of use
keywords.
As you may have noticed, namespaces and trait use
use
keywords, except for the import location where namespaces are imported outside the definition of the class, and trait is imported in the definition body of the class.
code is as follows |
copy code |
Note: P The HP interpreter copies the trait to the class's definition at compile time, but does not handle the incompatibilities introduced by the operation, and if trait assumes that there are specific properties or methods in the class, it is necessary to ensure that the class does have a corresponding property or method.
|