What is Trait?
php has supported Trait features since version 5.4. It is very similar to the Class class, and all common Trait features in the class can be implemented. Traits are not used to replace classes, but to "mix" into classes. Traits are designed to reduce the limitations of single-inheritance languages and allow developers to freely reuse method sets in independent classes within different hierarchies. The semantics of trait and class composition define a way to reduce complexity and avoid the typical problems associated with traditional multiple inheritance. For example, two abstract classes need to be inherited at the same time, which is a function that the PHP language does not support. Trait is to solve this problem. Or it can be understood that the inheritance class chain isolates the subclass from inheriting certain characteristics of the parent class, which is equivalent to calling the members of the Trait first if there is a Trait when using the characteristics of the parent class.
Declaration of Trait
You need to use the class keyword to declare a class. Of course, you must also use the trait keyword to declare a Trait. The characteristics of a class Traits are generally available. Trait supports modifiers such asfinal, static and abstract, so Trait also supports the use of abstract methods, class definition static methods, and of course attributes can also be defined. But Trait cannot be instantiated using new like a class, because Trait is used when mixed into a class and cannot be used alone. If we compare Interface and Trait, Trait will be more convenient.
The simple Trait declaration code is as follows:
Basic use of Trait
Different from classes, Trait cannot instantiate objects by itself and must be mixed into a class for use. It is equivalent to copying the members in the Trait to the class, and using the class just like using its own members. If you want to use Trait in a class. Traits need to be mixed into the class through the use keyword.
The code is as follows:
method1(); // 通过 dome1 对象,直接调用混入类 dome1 的成员方法 method1 $obj->method2(); // 通过 dome1 对象,直接调用混入类 dome1 的成员方法 method2 ?>
In the above example, the use keyword is used to mix the members in dome into the dome1 class. You can also use the use keyword to mix multiple Traits at once and use them together. By separating with commas, multiple Traits can be listed in the use statement, and they can all be inserted into a class. It should be noted that conflicts will inevitably occur when multiple Traits are used at the same time. php5.4 brings related keyword syntaxinsteadoffrom the syntax aspect.
The sample code is as follows:
fun(); // 输出第一个 Trait 中的 fun 方法 ?>
Not only can you use the use keyword in a class to mix members of Trait into the class, you can also use use in Trait Keyword mixes in members from another Trait. This forms a nesting between Traits. In order to enforce requirements on the classes used, Traits support the use of abstract methods. If you declare an abstract method that needs to be implemented in a Trait, the class that uses it must implement it first, just like inheriting an abstract class and must implement the abstract method in the class.
For more detailed usage, please refer to the official manual. But when you first start learning Trait, you should understand the following key points:1. Trait will override the parent class method of the calling class.
2. Members inherited from the base class are overridden by members inserted by Trait. The order of precedence is: members from the current class override Trait methods, and Traits override inherited methods.
3. Trait cannot use new to instantiate objects like a class.
4. A single Trait can be composed of multiple Traits.
5. In a single class, use use to introduce Trait. You can introduce multiple Traits.
6. Trait supports modifiers, such as final, static, and abstract.
7. You can use insteadof and as operators to resolve conflicts between Traits.
8. Using as syntax can also be used to adjust the access control of methods.
【Related tutorial recommendations】
1. "
php.cn Dugu Jiujian (4)-php video Tutorial》2. Video tutorial: Declaration and usage skills of
trait characteristics: Collection of class methods to achieve code reuse##3.
php practical video tutorial
The above is the detailed content of What is Trait? Sharing the declaration and usage tips of Trait in php. For more information, please follow other related articles on the PHP Chinese website!