Traits is a code reuse mechanism prepared for a single inheritance language similar to PHP. Trait allows developers to freely reuse the method set from PHP 5.4.0 to independent classes in different hierarchies to reduce the restrictions of a single inheritance language, PHP implements a method for code reuse, called traits.
Traits is a code reuse mechanism prepared for a single inheritance language similar to PHP. Trait allows developers to freely reuse method sets in independent classes in different hierarchies to reduce the restrictions of a single inheritance language. The semantics of Traits and class combination defines a way to reduce complexity and avoid typical problems related to traditional multi-inheritance and mixed classes (Mixin.
Trait is similar to a class, but it is only intended to combine functions in a fine-grained and consistent manner. Trait cannot be instantiated by itself. It adds a combination of horizontal features for traditional inheritance; that is, members of the application class do not need to inherit.
Trait is added to PHP5.4, which is neither an interface nor a class. The main purpose is to solve the restrictions of the single inheritance language. It is a solution for PHP multi-inheritance. For example, it is very troublesome to inherit two Abstract classes at the same time. Trait is designed to solve this problem. It can be added to one or more existing classes. It declares what the class can do (indicating its interface features), and also contains specific implementations (indicating its class features)
Easy to use
First, of course, we declare a Trait. the trait keyword is added in PHP5.4.
trait first_trait {function first_method() { /* Code Here */ }function second_method() { /* Code Here */ }}
If you want to use this Trait in the Class, use the use keyword.
Class first_class {// pay attention to this line and declare to use first_traituse first_trait;} $ obj = new first_class (); // Executing the method from trait $ obj-> first_method (); // valid $ obj-> second_method (); // valid
Use multiple Trait
Multiple Trait instances 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(); // Print : method
Nesting between Trait
Meanwhile, Trait can also be nested with each other, for example
trait first_trait {function first_method() { echo "method"; }}trait second_trait {use first_trait;function second_method() { echo "method"; }}class first_class {// now using 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 the abstract method to be implemented in Trait so that the Class that uses it must implement it.
Trait first_trait {function first_method () {echo "method" ;}// here you can add modifiers, indicating that the call class must implement the abstract public function second_method ();} class first_method {use first_trait; function second_method () {/* Code Here */}}
Trait conflict
When multiple Trait nodes are used at the same time, they will inevitably conflict with each other. we need to solve this problem. PHP5.4 introduces the related keyword syntax in terms of syntax: insteadof and as. for usage, see
Trait first_trait {function first_function () {echo "From First Trait" ;}} trait second_trait {// The name here is the same as first_trait, and there will be conflicting function first_function () {echo "From Second Trait" ;}} class first_class {use first_trait, second_trait {// declare here to use first_trait's first_function to replace // first_trait declared in second_trait :: first_function insteadof second_trait; }}$ obj = new first_class (); // Output: From First Trait $ obj-> first_function ();
The above are some basic usage of Trait. For more details, refer to the official manual. Here are some notes:
Trait overwrites the parent class method inherited by the call class.
Trait cannot be instantiated using new like Class
A single Trait can be composed of multiple Trait
In a single Class, you can use multiple Trait
Trait supports modifiers, such as final, static, and abstract
We can use the insteadof and as operators to solve Trait conflicts.