What is the role of trait in PHP?

Source: Internet
Author: User
Tags modifiers traits
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 method sets in separate classes within different hierarchies

Since PHP 5.4.0, PHP has implemented a method of code reuse, called traits.

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).

Trait is similar to a class, but only designed to combine functionality in a fine-grained and consistent way. Trait cannot be instantiated by itself. It adds a combination of horizontal attributes to traditional inheritance, meaning that members of the application class do not need inheritance.

Trait is added in PHP5.4, which is neither an interface nor a class. The main purpose is to solve the limitations of single-inheritance language. is a solution to PHP multiple inheritance. For example, it would be cumbersome to inherit two Abstract classes at the same time, Trait to solve the problem. It can be added to one or more existing classes. It declares what the class can do (indicating its interface characteristics), and also contains a concrete implementation (indicating its class characteristics)

Simple to use

First of all, of course declaring a trait,php5.4 adds the Trait keyword

Trait first_trait {function First_method () {/* code here */}function Second_method () {/* code here/}}

Also, if you want to use the Trait in Class, use the Using keyword

Class First_class {//Note this line, declaring using First_traituse first_trait;} $obj = new First_class ();//Executing the method from Trait$obj->first_method (); Valid$obj->second_method (); Valid

Using multiple Trait

Multiple Trait 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

At the same time, Trait can also be nested between 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 with 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 in Trait the abstract method that needs to be implemented, so that the Class that uses it must implement it

Trait first_trait {function First_method () {echo "method";} The modifier can be added here, stating that the calling class must implement its abstract public function Second_method ();} Class First_method {use First_trait;function Second_method () {/* Code here */}}

Trait conflict

The simultaneous use of multiple Trait will inevitably conflict, which requires us to solve. PHP5.4 brings the relevant keyword syntax from the syntax: Insteadof and AS, use see

Trait first_trait {function first_function () {echo "From First trait";}} Trait second_trait {//The name here is the same as first_trait, there will be conflict function first_function () {echo "from second trait";}} Class First_class {Use First_trait, second_trait {//declared here using first_trait first_function replacement//second_trait T::first_function insteadof second_trait;}} $obj = new First_class ();//Output:from First trait$obj->first_function ();

Above is a few Trait more basic use, more detailed can refer to the official manual. Here is a summary of the points to note:

Trait overrides the parent class method that invokes the class inheritance

Trait cannot use new instantiation as Class

A single Trait can be made up of multiple Trait

In a single Class, you can use multiple Trait

Trait supports modifiers (modifiers), such as final, static, abstract

We can use the insteadof and as operators to resolve conflicts between Trait

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.