PHP Trait Features

Source: Internet
Author: User

Trait is a new code reuse mechanism that PHP has added since version 5.4, and is a method of fine-grained code reuse. The official documentation for trait gives the following explanations:

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

Trait 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 in separate classes within different hierarchies. The semantics of Trait and class combinations define a way to reduce complexity, avoiding traditional multi-inheritance and Mixin class-related typical problems.

Trait is similar to Class, but only designed to combine functionality in a fine-grained and consistent way. cannot be instantiated by trait itself. It adds a combination of horizontal features to the traditional inheritance, meaning that no inheritance is required between several classes of the application.

Trait word interpretation for the characteristics, very good explanation of the characteristics of trait this method, can be in different hierarchies of independent classes in the reuse of methods and properties, in an easy-to-understand way to illustrate, in the online game "World of Warcraft", Mage, Stalker, Warrior is three players can control the occupation, Three occupations are equal, can be understood as inheriting from the "role" of the parent class, because no matter what occupation the player can use the mount, so the parent class "Character" can add a ride () method, indicating the use of mounts, so that all inherit from "Character" Have the ability to use mounts. and different professions have their own characteristics, for example, the mage can use the snowstorm, the stalker can sneak, the warrior can charge, therefore, "Mage" class can be added "blizzard" method to indicate the mage use blizzard skills, "rogue" class to add "stealth" The method indicates that the stalker uses stealth skills, and the "charge" method in the "Worrior" class indicates that the warrior uses the charge skill, so that the characteristics of each profession can be manifested. And when the allocation of equipment, the problem comes, well-known "World of Warcraft" equipment system is very large, armor is divided into cloth armor, leather, lock armor, plate armor four different types, and different professions can be equipped with the type of armor is not exactly the same, some occupations can only be equipped with a type of armor, While some professions can equip most types of armor, trait can come in handy in order to avoid duplication of definitions. Here we define "cloth armour", "Leather Armor" (Leather armour), "Plate armour" (plate armour) three different trait, indicating that a profession can be equipped with a certain armor, so that when we define a career, It is possible to define the type of armor that this profession can equip by inserting trait, which is independent of the occupation, and many professions can share a feature, for example, a warrior can be equipped with a plate armor or a cloth armor, so that when declaring a warrior this class, it can insert many properties, To indicate that a warrior is possessed as a condition of a full-demand party:

1 Trait Clotharmour2 {3      Public functionPutonclotharmour ()4     {5     //put on a cloth armor6     }7 }8 Trait Leatherarmour9 {Ten      Public functionPutonleatherarmour () One     { A     //put on a piece of leather armor -     } - } the Trait Platearmour - { -      Public functionPutonplatearmour () -     { +     //put on a piece of board armor -     } + } A classCharacter at { -      Public functionRide () { -     //Summon Mounts -     } - } - //Mage in classMageextendsCharacter - { to      UseClotharmour; +      Public functionBlizzard () { -     //use "Blizzard" the     } * } $ //WarriorPanax Notoginseng classWorriorextendsCharacter - { the      UseClotharmour,leatherarmour,Platearmour; +      Public functioncharge () { A     //use "Charge" the     } + } - //Stealth Person $ classRogueextendsCharacter $ { -      UseClotharmour,Leatherarmour; -      Public functionStealth () { the     //use "Sneak" -     }Wuyi } the $worrior=NewWorrior (); - //inherit the method from the parent class, summon the Mount Wu $worrior-Ride (); - //own method, using the charge skill About $worrior-charge (); $ //Warriors can equip cloth Armor - $worrior-Putonclotharmour (); - //warriors can equip leather - $worrior-Putonleatherarmour (); A  //warriors can equip plate armor + $worrior->putonplatearmour ();

In this case, you can see the most basic use of trait, trait is actually a set of properties and methods that describe a certain feature set, can be easily combined and applied, low coupling, easy to maintain, At the same time, the introduction of features also to some extent reduce the level of inheritance after the need to constantly trace the parent to find some methods of the situation, improve the readability of the Code for improving the efficiency of development work and development team work is very good.

PHP Trait Features

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.