PHP code reuse traits simple definition and usage introduction

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

<?phptrait Ezcreflectionreturninfo {    function Getreturntype () {/*1*/}    function Getreturndescription () {/* 2*/}}class Ezcreflectionmethod extends Reflectionmethod {use    ezcreflectionreturninfo;    /* */}class Ezcreflectionfunction extends Reflectionfunction {use    ezcreflectionreturninfo;    /* ... */}?>

Priority level

A member inherited from a base class is overwritten 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

<?phpclass Base {public    function SayHello () {        echo ' Hello ';    }} Trait Sayworld {public    function SayHello () {        Parent::sayhello ();        Echo ' world! ';    }} Class Myhelloworld extends Base {use    Sayworld;} $o = new Myhelloworld (); $o->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

<?phptrait HelloWorld {public    function SayHello () {        echo ' Hello world! ';    }} Class Theworldisnotenough {use    HelloWorld;    Public Function SayHello () {        echo ' Hello universe! ';    }} $o = new Theworldisnotenough (); $o->sayhello ();? >

The above routines will output: Hello universe!

Multiple trait

Separated by commas, multiple trait are listed in the use declaration and can be inserted into a class.

Examples of the use of multiple trait

<?phptrait Hello {public    function SayHello () {        echo ' hello ';    }} Trait World {public    function Sayworld () {        echo ' world ';    }} Class Myhelloworld {Use    Hello, world;    Public Function Sayexclamationmark () {        echo '! ';    }} $o = new Myhelloworld (); $o->sayhello (); $o->sayworld (); $o->sayexclamationmark ();? >

The above routines will output: Hello world!

Resolution of conflicts

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

<?phptrait A {public    function SmallTalk () {        echo ' A ';    }    Public Function Bigtalk () {        echo ' A ';    }} Trait B {public    function SmallTalk () {        echo ' B ';    }    Public Function Bigtalk () {        echo ' B ';    }} Class Talker {Use    A, B {        b::smalltalk insteadof A;        A::bigtalk insteadof B;    }} Class Aliased_talker {Use    A, B {        b::smalltalk insteadof A;        A::bigtalk insteadof B;        B::bigtalk as Talk;    }}? >

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.

Modify access control for a method

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

Example of modifying access control for a method

<?phptrait HelloWorld {public    function SayHello () {        echo ' Hello world! ';    }} Modify SayHello access Control class MyClass1 {use    HelloWorld {SayHello as protected;}} Give method a changed access control alias//original SayHello access control is not changed class MyClass2 {use    HelloWorld {SayHello as Private Myprivatehello;}} ?>

From trait to form 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

<?phptrait Hello {public    function SayHello () {        echo ' hello ';    }} Trait World {public    function Sayworld () {        echo ' world! ';    }} Trait HelloWorld {Use    Hello, world;} Class Myhelloworld {use    HelloWorld;} $o = new Myhelloworld (); $o->sayhello (); $o->sayworld ();? >

The above routines will output: Hello world!

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.