What is trait? Trait in PHP and sharing of usage tips

Source: Internet
Author: User
Tags php language modifiers
What is trait?

PHP supports the Trait feature from version 5.4 and is similar to class classes, and the general feature Trait in the class can be implemented. Trait is not intended to replace classes, but to "mix in" classes. Trait is designed to reduce the limitations of single-inheritance languages, allowing developers to freely reuse the set of methods in separate classes within different hierarchies. The semantics of Trait and class combinations are defined as a way to reduce complexity and avoid typical problems associated with traditional multi-inheritance. For example, it is necessary to inherit two abstract classes at the same time, which is a feature not supported by the PHP language, Trait is to solve this problem. Or it can be understood that some of the attributes of the subclass inheriting the parent class are isolated in the inheritance class chain, which is equivalent to using the attributes of the parent class, and if there is a Trait, the members of Trait are called preferentially.

Trait's statement

Declaring a class requires the use of the Class keyword, which declares that Trait must also use the Trait keyword, and that the class has features Trait generally. Trait supports final, static, and abstract modifiers, so Trait also supports the use of abstract methods, class-defined static methods, and, of course, the ability to define properties. However, Trait cannot use new instantiation as a class, because Trait is used in a class and cannot be used alone. If you take Interface and Trait analogy, Trait will have more convenient places.

The simple Trait declaration code is as follows:

<?php//Use the TRAIT keyword to declare a trait, need to php5.4 the above version trait dome{public $a = true;        Declare member property static $b = 1;           Declare a static variable using the static keyword function method1 () {}      //Declare member Method abstract public Function method2 ();//Add abstract modifier, stating that the calling class must implement it}?>


Basic use of Trait

Unlike a class, Trait cannot instantiate an object by itself, and must be used in a class. The equivalent of copying members from a Trait into a class, as you would with your own members when you apply a class. If you want to use Trait in your class. The Trait needs to be mixed into the class with the USE keyword.

Its code is as follows:

<?php//using the TRAIT keyword to declare a trait, there are two member methods trait dome{function Method1 () {}      //Declaring Member Method function Method2 () {}      // Declares a member method}class dome1{    //Declares a class in which a class is mixed with Trait use dome;    Use the Use keyword in a class using Dome} $obj = new dome1 ();   Instantiate the Dome1 object $obj->method1 ();      By Dome1 object, the member method mixed with class dome1 is called directly method1$obj->method2 ();      The Member method that is mixed with the class dome1 is called directly through the Dome1 object method2?>

In the example above, with the USE keyword, the members in the dome are mixed in the Dome1 class. It can also be used with multiple Trait at once with the use keyword. By separating the commas, multiple Trait are listed in the use declaration and can be inserted into a class. It is important to note that the simultaneous use of multiple Trait will inevitably conflict. php5.4 from the grammatical aspects of the relevant keyword syntax insteadof .

The sample code looks like this:

<?php//using the TRAIT keyword to declare a trait, there are two member methods trait dome1{function Fun () {     echo "the Fun method in the first trait";}} Trait dome2   //The same name here will conflict {function fun () {    echo "the Fun method in the second trait";}} Class dome{use dome1,dome2{     //dome2 declaration   dome1::fun insteadof dome2;  Affirm to use the fun substitution in dome1}} $obj = new Dome (); $obj->fun ();  output The Fun method in the first Trait?>

Not only can you use the Using keyword in a class to mix members in Trait into a class, but you can also use the Using keyword in Trait to mix members in another Trait. This creates a nesting between the Trait. In order to impose mandatory requirements on the classes used, Trait supports the use of abstract methods. If you declare an abstract method that needs to be implemented in Trait, you can make the class that uses it must implement it first, just as you would inherit an abstract class, and you must implement an abstract method in the class.

For more detailed use, refer to the official manual. But beginning to learn Trait should know the following points :

1. Trait overrides the parent class method of the calling class.

2. A member inherited from a base class is overwritten by a member inserted by Trait. The order of precedence is that the members from the current class override the Trait method, and Trait overrides the inherited method.

3. Trait cannot instantiate an object using new as a class.

4. A single Trait can consist of multiple Trait.

5. In a single class, using use to introduce Trait, you can introduce multiple.

6. Trait supports modifiers, such as final, static, abstract.

7. You can use the insteadof and as operators to resolve conflicts between trait.

8. Using the AS syntax can also be used to adjust the access control of the method.


"Related tutorials Recommended"

1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"

2. Video tutorial: Trait feature declaration and usage tips: A collection of class methods that implement code reuse

3. PHP real-Combat video tutorial

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.