This article mainly introduces the Traits function introduced by the thinkPHP5.0 framework, and analyzes the concept and function of Traits and the usage of Traits function in thinkPHP5.0 in the form of instances, for more information, see the next article. This article mainly introduces the Traits function introduced by thinkPHP5.0 framework, and analyzes the concepts and functions of Traits and the usage of Traits in thinkPHP5.0 based on examples, for more information, see
This example describes the Traits function introduced by the thinkPHP5.0 framework. We will share this with you for your reference. The details are as follows:
ThinkPHP 5.0 began to adopt the trait function (PHP5.4 +) as an extension mechanism, which can easily implement multiple inheritance issues of a class library.
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.
However, because PHP5.4 does not support automatic trait loading, if PHP5.4 is used, you must manually import the trait class library. The system provides an assistant function load_trait to automatically load the trait class library. for example, you can correctly introduce the trait class library.
Namespace app \ index \ controller; load_trait ('Controller/Jump '); // introduce traits \ controller \ Jumpclass index {use \ traits \ controller \ Jump; public function index () {$ this-> assign ('name', 'value'); $ this-> show ('index ');}}
If your PHP version is later than 5.5, you can omit the load_trait function to introduce trait.
namespace app\index\controller;class index{ use \traits\controller\Jump; public function index() { }}
Multiple trait class libraries can be introduced at the same time, for example:
namespace app\index\controller;load_trait('controller/Other');load_trait('controller/Jump');class index{ use \traits\controller\Other; use \traits\controller\Jump; public function index() { }}
Or use
namespace app\index\controller;load_trait('controller/Other');load_trait('controller/Jump');class index{ use \traits\controller\Other,\traits\controller\Jump; public function index() { }}
The system provides encapsulated trait class libraries, which are mainly used to expand controllers and model classes. The root namespace of these built-in trait class libraries uses traits instead of trait to avoid conflicts with system keywords.
Note the priority of class libraries introduced by trait. the inherited members of the base class are overwritten by the members inserted by trait. The priority is that the members of the current class overwrite the trait method, while the trait method overwrites the inherited method.
Trait classes do not support defining constants of classes. attributes defined in trait cannot be redefined in the current class or inherited class.
Conflict resolution
We can introduce multiple trait class libraries in a class library. if both trait classes define a method with the same name, a fatal error will occur if the conflict is not explicitly resolved.
To resolve naming conflicts between multiple trait classes, you must use the insteadof operator to specify which of the conflicting methods to use.
The preceding method can only exclude other methods. the as operator can introduce one conflicting method with another name.
The above is the details of the instance code sharing that the thinkPHP5.0 framework introduces the Traits function. For more information, see other related articles in the first PHP community!