PHP Trait Concise tutorial, multi-Inheritance implementation scheme

Source: Internet
Author: User
Tags traits
http://php.net/manual/en/language.oop5.traits.php

Since PHP5.4.0, PHP has implemented a method of code reuse, called Traints.

Traits is a code reuse mechanism that is prepared for PHP-like single-inheritance speech. Trait to reduce the limitations of single-inheritance statements, developers are free to reuse the set of methods in separate classes within different hierarchies. The semantics of traits and class combinations is to define a method to reduce complexity and avoid typical problems associated with traditional inheritance and mixing classes (Mixin).

Traits is similar to a class, but only designed to combine functionality in a fine-grained and one-way manner. 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.

In my understanding, it is in the inheritance class chain that the subclass inherits some of the attributes of the parent class (that is, when the subclass "wants to use the attributes of the parent class", if trait has one, the trait method, the property, and so on) is isolated.

  
   Var. Php_eol;    }    function A ()    {        echo "a". Php_eol;}    } Interface myinterface{    function __construct ();    function B ();} Abstract class myabstract{    protected $var 2 = "Myabstract_var";    Use mytrait;    Function B ()    {        echo "B". Php_eol;}    } Class MyClass extends Myabstract implements myinterface{    protected $var 3 = "Myclass_var";    can also be referenced here, do not distinguish between the inheritance relationship    //use mytrait    function C ()    {        echo "C". Php_eol;}    } $class = new MyClass (); $class->a (); $class->b (); $class->c ();




Output Result:

Summary :

① Essentially, the concept of trait and include files is almost

②trait can be more convenient for code reuse, because we implement the private property and method of the subclass in the parent class, and the trait is the same as the effect of writing the code directly in the object.

③ when using trait, you should resolutely avoid naming conflicts, especially when using multiple trait at the same time.

④ If a naming conflict arises, if the visibility, initial value, or static of the two are exactly the same, the trait will overwrite the object and throw a e_strict error, otherwise it will throw a e_compile_error error and terminate the compilation.

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