How to use the latest PHP trait

Source: Internet
Author: User
To be popular, PHP uses the Trait keyword in order to solve a class that wants to integrate both the properties and methods of the base class, as well as other base classes, and trait is typically used in conjunction with use.

Let's take a look at the use of trait in PHP as described in this article

<?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?

<?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:

The code is as follows:

PHP Fatal Error:  Trait method Hello has not been applied, because there is collisions with other Trait methods on Cl Ass1 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:

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

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.