A simple tutorial for reusing PHP code

Source: Internet
Author: User
Tags traits
Since PHP 5.4.0, PHP has implemented a code reuse method called Trait.

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

Simple to use

<?phptrait test{public    function Echohello ()    {        echo ' Hello Trait ';    }} Class base{public    function Index ()    {        echo ' index ';    }} Class One extends base{use    Test;} Class extends base{use    Test;} $one = new One (), $two = new (), Echo $one->echohello (), Echo $one->index (), Echo $two->echohello ();

The result output is Hello Trait index Hello Trait.

Members that inherit from the base class are overwritten by the members that are inserted by Trait. Precedence is the method from which the member of the current class overrides the Trait, and Trait overrides the inherited method.

<?phptrait test{public    function Echohello ()    {        echo ' Hello Trait ';    }} Class Base {use    Test;    Public Function Echohello ()    {        echo ' Hello Base ';    }} Class One extends Base  {use    Test;    Public Function Echohello ()      {        echo ' Hello one ';    }} Class extends Base  {use    Test;} $one = new One (), $two = new (), $base = new base ()  ; echo $one->echohello (); Echo $two->echohello (); Echo $base->echohello (); result output Hello one hello Trait hello base.? >

The class One example overrides the base class and Trait Test, stating that the methods of the current class take precedence over them.

The class One example overrides the base class, and the Trait has precedence over the inherited base class.

The class Base example overrides Trait Test, which shows that the method of the current class takes precedence over Trait.

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

<?phptrait test{public    function Echohello ()    {        echo ' Hello ';    }} Trait testtwo{public    function Echoword ()    {        echo ' word! ';    }} Class one{use    test,testtwo;} $one  = new One (); Echo $one->echohello (); Echo $one->echoword ();

Result output Hello Word!.

If two Trait all insert a method with the same name, a fatal error will occur if the conflict is not resolved explicitly.

<?phptrait test{public    function Echohello ()    {        echo ' Hello Test ';    }    Public Function Echoword ()    {        echo ' word Test ';    }} Trait testtwo{public    function Echohello ()    {        echo ' Hello testtwo ';    }    Public Function Echoword ()    {        echo ' word testtwo ';    }} Class one{use    Test, testtwo {        test::echohello as echotest;        Test::echoword insteadof Testtwo;        Testtwo::echohello insteadof Test;}    } $one = new One (), Echo $one->echotest (), Echo $one->echoword (), Echo $one->echohello ();

Output: Hello test word test Hello Testtwo.

Use as as the alias, i.e. Test::echohello as echotest; Outputs the Echohello in Trait Test.

Use the INSTEADOF operator to exclude other Trait, namely Test::echoword insteadof testtwo; The output is word test, using the Echoword in Trait test

Modify control permissions for a method

<?phptrait test{public    function Echohello ()    {        echo ' Hello ';    }    Public Function Echoword ()    {        echo ' word ';    }} Trait testtwo{public    function Echohello ()    {        echo ' Hello testtwo ';    }    Public Function Echoword ()    {        echo ' word testtwo ';    }} Class one{use    Test {        Echohello as private;    }} Class two{use    Test {        Echohello as private echotwo;    }} $one = new One (), $two = new (), Echo $two->echohello ();

The result of the output Hello.

The Echohello is set to private using as in class one, and Echohello is not accessible through class one.

Use as in Class A to rename it first, and then set the new named method to private, and the methods in the original Trait can be accessed normally.

You can also define properties like classes in Trait. is very useful!

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 be instantiated with new as class

A single Trait can consist of multiple Trait, and in a single Class you can use multiple trait,trait support modifiers (modifiers), such as final, static, abstract, and we can use INSTEADOF and as 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.