This article mainly introduced the thinkPHP5.0 framework introduced traits function, combined with the example form analysis traits concept, function and thinkPHP5.0 traits function use method, need friends can refer to the next
This paper describes the introduction of the thinkPHP5.0 framework into the traits function. Share to everyone for your reference, as follows:
thinkphp 5.0 starts with the trait function (php5.4+) as an extension mechanism, which can easily realize the multiple inheritance problem of a class library.
Traits is a code reuse mechanism that is prepared for PHP-like single-inheritance languages. Trait to reduce the limitations of single-inheritance languages, developers are free to reuse the set of methods in separate classes within different hierarchies. The semantics of traits and class combinations are defined as a way to reduce complexity and avoid typical problems associated with traditional multi-inheritance and mixed-Class (Mixin).
However, since the PHP5.4 version does not support trait automatic loading, if it is a PHP5.4 version, the Trait class library must be manually imported, and an assistant function load_trait is provided to automatically load the Trait class library, for example, the Trait class library can be introduced correctly.
Namespace app\index\controller;load_trait (' Controller/jump '); Introduction of Traits\controller\jumpclass index{use \traits\controller\jump; Public Function Index () { $this->assign (' name ', ' value '); $this->show (' Index ');} }
If your PHP version is greater 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 () { }}
Can support the introduction of multiple trait class libraries 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 some well-packaged trait class libraries, mainly for the expansion of controllers and model classes. The root namespace of the Trait class library built into these systems takes traits instead of trait because it avoids conflicts with the system's keywords.
The class library introduced by the trait method requires attention to precedence, and members inherited from the base class are overwritten by the members that are trait inserted. Precedence is the method from which the member of the current class overrides the trait, and trait overrides the inherited method.
Constants that define classes are not supported in the trait class, and properties defined in trait cannot be redefined in the current class or in the inherited class.
Resolution of conflicts
We can introduce multiple trait class libraries in a class library, and if two trait all define a method with the same name, a fatal error will occur if the conflict is not resolved explicitly.
In order to resolve the naming conflicts of multiple trait in the same class, it is necessary to use the INSTEADOF operator to explicitly specify which of the conflicting methods to use.
The above method only allows other methods to be excluded, and the as operator can introduce one of the conflicting methods with another name.