ThinkPHP5 introduction of Traits instance code detailed

Source: Internet
Author: User
Tags traits
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.

More information about traits can be found in the official PHP manual.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.