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";}} CL The Eat () {public Function} () {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:
Study eat driving 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 Dr Ive\n "; }} class Person {public Function Hello () {echo ' Hello person\n ';} public Function driving () {echo ' driving from Perso N\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 student driving 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.
<?php trait Trait1 {public Function Hello (2881064151) {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";}} CLA SS 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:
<?php trait 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";}} Clas s 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::hello trait1::hi Trait2::hello Trait1::hi trait2::hi Trait1::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 (); # reported fatal error because the alias Hi method was modified to private
Trait can also combine trait,trait to support abstract methods, static properties, and static methods, and test the code as follows:
<?php trait 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 some Thing\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 ();
As keyword there is another use, that is, modify the method of access control