Traits in PHP, detailed phptraits_php tutorial

Source: Internet
Author: User
Tags traits

Traits in PHP, detailed phptraits


PHP is a single-inheritance language, and PHP's class cannot inherit properties or methods from two base classes at the same time before PHP 5.4 traits appears. PHP's traits is similar to the Go language, which declares the trait name to be combined by using the USE keyword in a class, whereas a trait declaration uses trait keywords and trait cannot be instantiated directly. For specific usage, see the following code:

<?php  Trait Drive {public    $carName = ' trait ';    Public Function driving () {      echo "driving {$this->carname}\n";    }  }  Class Person {public    function eat () {      echo "eat\n";    }  }  Class Student extends person {use drive    ;    Public Function Study () {      echo "study\n";    }  }  $student = new Student ();  $student->study ();  $student->eat ();  $student->driving ();

The output results are as follows:

Studyeatdriving Trait

In the above example, the student class inherits the person and has the Eat method, which, by combining drive, has the driving method and the property carname.

If a property or method of the same name exists in the trait, base class, and this class, which one will eventually be retained? Test it with the following code:

<?php   Trait Drive {public    function hello () {      echo "Hello drive\n";    }    Public Function driving () {      echo ' driving from drive\n ';    }  }  Class Person {public    function hello () {      echo "Hello person\n";    }    Public Function driving () {      echo ' driving from person\n ';    }  }  Class Student extends person {use drive    ;    Public Function Hello () {      echo "Hello student\n";    }  }  $student = new Student ();  $student->hello ();  $student->driving ();

The output results are as follows:

Hello studentdriving from Drive

It is therefore concluded that when a method or property has the same name, the methods in the current class override the Trait method, and the trait method overrides the method in the base class.

If you want to combine multiple trait, separate the trait name by commas:

Use Trait1, Trait2;

What happens if more than one trait contains a method or property of the same name? The answer is that when multiple trait of a combination contain a property or method with the same name, you need to explicitly declare a conflict resolution, or a fatal error will occur.

<?phptrait Trait1 {public  function hello () {    echo "trait1::hello\n";  }  Public Function Hi () {    echo "trait1::hi\n";  }} Trait Trait2 {public  function hello () {    echo "trait2::hello\n";  }  Public Function Hi () {    echo "trait2::hi\n";  }} Class Class1 {Use  Trait1, Trait2;}

The output results are as follows:

PHP Fatal error:trait Method Hello have not been applied, because there is collisions with other Trait methods on Class1 In ~/php54/trait_3.php on line 20

Using the insteadof and as operators to resolve conflicts, Insteadof uses a method instead of another, and as is an alias for the method, see the code for details:

<?phptrait Trait1 {public  function hello () {    echo "trait1::hello\n";  }  Public Function Hi () {    echo "trait1::hi\n";  }} Trait Trait2 {public  function hello () {    echo "trait2::hello\n";  }  Public Function Hi () {    echo "trait2::hi\n";  }} Class Class1 {Use  Trait1, Trait2 {    trait2::hello insteadof Trait1;    Trait1::hi insteadof Trait2;  }} Class Class2 {Use  Trait1, Trait2 {    trait2::hello insteadof Trait1;    Trait1::hi insteadof Trait2;    Trait2::hi as Hei;    Trait1::hello as hehe;  }} $OBJ 1 = new Class1 (); $Obj 1->hello (); $Obj 1->hi (); echo "\ n"; $Obj 2 = new Class2 (); $Obj 2->hello (); $Obj 2->hi () ; $Obj 2->hei (); $Obj 2->hehe ();

The output results are as follows:

Trait2::hellotrait1::hitrait2::hellotrait1::hitrait2::hitrait1::hello

As keyword there is another use, that is to modify the method of access control:

<?php  Trait Hello {public    function hello () {      echo "hello,trait\n";    }  }  Class Class1 {Use    hello {      hello as protected;    }  }  Class Class2 {use    Hello {      Hello::hello as private hi;    }  }  $OBJ 1 = new Class1 ();  $OBJ 1->hello (); # reported fatal error because the Hello method was modified to protected  $OBJ 2 = new Class2 ();  $OBJ 2->hello (); # The original Hello method is still public  $Obj 2->hi (); # fatal error, because the alias Hi method is changed to private

Trait can also combine trait,trait to support abstract methods, static properties, and static methods, and test the code as follows:

<?phptrait Hello {public  function SayHello () {    echo "hello\n";  }} Trait World {use  Hello;  Public Function Sayworld () {    echo "world\n";  }  Abstract public Function Getworld ();  Public Function Inc () {    static $c = 0;    $c = $c + 1;    echo "$c \ n";  }  public static function DoSomething () {    echo "Doing something\n";  }} Class HelloWorld {use World  ;  Public Function Getworld () {    return ' get World ';  }} $OBJ = new HelloWorld (); $Obj->sayhello (); $Obj->sayworld (); Echo $Obj->getworld (). "\ n"; HelloWorld::d osomething (); $Obj->inc (); $Obj->inc ();

The output results are as follows:

Helloworldget worlddoing Something12

The above is the whole content of this article, I hope that everyone's study has helped.

http://www.bkjia.com/PHPjc/1039182.html www.bkjia.com true http://www.bkjia.com/PHPjc/1039182.html techarticle Traits in PHP, detailed phptraits PHP is a single-inheritance language, PHP 5.4 traits before the advent of the class can not be inherited from two base classes of properties or methods. PHP traits and go language ...

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