PHP Learning--traits new features _php tutorials

Source: Internet
Author: User
Tags traits

New features of PHP learning--traits


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 Example A member inherited from a base class is overridden by a member inserted by trait. Precedence is the method from which the member of the current class overrides the trait, and trait overrides the inherited method. Example of precedence order SayHello ();? > The above routines will output: Hello world! Members inherited from the base class are overwritten by the SayHello method in the inserted Sayworld Trait. Its behavior is consistent with the methods defined in the Myhelloworld class. The precedence is that methods in the current class override the Trait method, and the trait method overrides the method in the base class. Another example of priority order SayHello ();? > The above routines will output: Hello universe! Multiple trait are separated by commas, and multiple trait are listed in the use declaration, which can be inserted into a class. Examples of the use of multiple trait SayHello (); $o->sayworld (); $o->sayexclamationmark ();? > The above routines will output: Hello world! Conflict resolution If two trait all insert 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. Examples of conflict resolution In this example, Talker uses trait A and B. Because A and B have conflicting methods, they define the use of smallTalk in trait B and the bigtalk in trait a. Aliased_talker uses the as operator to define talk as an alias for the bigtalk of B. Modifying the access control of a method using the as syntax can also be used to adjust the access control of the method. Example of modifying access control for a method From trait to compose trait just as classes can use trait, other trait can also use trait. When trait is defined, it can combine some or all of the members of another trait by using one or more trait. Examples of trait from trait SayHello (); $o->sayworld ();? > The above routines will output: Hello world! Abstract members of Trait in order to impose mandatory requirements on the classes used, Trait supports the use of abstract methods. An example of an abstract method to make a mandatory request Getworld (); } Abstract Public Function Getworld ();} Class Myhelloworld {private $world; use Hello, Public function Getworld () {return $this->world;} public Function set World ($val) {$this->world = $val;}}? The static member traits of the > Trait can be defined by static member static methods. Examples of static variables Inc (); echo 1$p = new C2 (); $p->inc (); Examples of echo 1?> static methods Examples of static variables and static methods Property trait can also define properties. Examples of defining attributes x;? > If trait defines a property, the class will not be able to define properties of the same name, or an error will result. If the attribute's definition in the class is compatible with the definition in trait (the same visibility and initial value), the error level is e_strict, otherwise it is a fatal error. Examples of conflicts Examples of different use of use The first use is used Foo\test for namespace, the \foo\test is found, the second uses is a trait, and the \foo\bar\foo\test is found. __class__ and __trait____class__ return the CLASS name,__trait__ of use TRAIT return TRAIT name TestMethod (); Class:baseclass//trait:testtrait Trait Single Case name = ' Foo '; }} class Bar {use singleton; private Function __construct () {$this->name = ' Bar ';}} $foo = Foo::getinstance (); echo $ foo->name; $bar = Bar::getinstance (); Echo $bar->name; Calling the trait method is not obvious, but if the trait method can be defined as a static method in the normal class, it can be invoked as follows

http://www.bkjia.com/PHPjc/927607.html www.bkjia.com true http://www.bkjia.com/PHPjc/927607.html techarticle PHP Learning--traits new features since PHP 5.4.0, PHP implements a method of code reuse, called traits. Traits is a code reuse that is prepared for PHP-like single-Inheritance languages ...

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