PHP Traits detailed description of the original link: http://tabalt.net/blog/php-traits/
PHP is a single-inherited language. before the emergence of PHP 5.4 Traits, PHP classes cannot inherit attributes or methods from both base classes. Php's Traits and Go language combination functions are similar. you can use the use keyword in the class to declare the Trait name to be combined. a specific Trait declaration uses the trait keyword, and Trait cannot be directly instantiated. For specific usage, see the following code:
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 result is as follows:
studyeatdriving trait
In the preceding example, the Student class inherits the Person, has the eat method, and combines Drive with the driving method and the carName attribute.
Which of the following attributes or methods will be retained if Trait, base class, and current class have an attribute or method of the same name? Test the following code:
hello(); $student->driving();
The output result is as follows:
hello studentdriving from drive
Therefore, it is concluded that when the method or attribute has the same name, the method in the current class will overwrite the trait method, and the trait method overwrites the method in the base class.
If you want to combine multiple Trait, use commas to separate the Trait names:
use Trait1, Trait2;
What if multiple Trait nodes contain methods or attributes with the same name? The answer is that when multiple Trait combinations contain attributes or methods of the same name, you need to explicitly resolve the conflict. Otherwise, a fatal error will occur.
The output result is as follows:
PHP Fatal error: Trait method hello has not been applied, because there are collisions with other trait methods on Class1 in ~/php54/trait_3.php on line 20
Use the insteadof and as operators to resolve conflicts. insteadof uses a method to replace the other, while as uses an alias for the method. for specific usage, see the code:
hello();$Obj1->hi();echo "\n";$Obj2 = new Class2();$Obj2->hello();$Obj2->hi();$Obj2->hei();$Obj2->hehe();
The output result is as follows:
Trait2::helloTrait1::hiTrait2::helloTrait1::hiTrait2::hiTrait1::hello
The as keyword has another purpose, that is, the access control of the modification method:
Hello (); # a fatal error is reported because the hello method is changed to protected $ Obj2 = new Class2 (); $ Obj2-> hello (); # The original hello method is still public $ Obj2-> hi (); # a fatal error is reported because the alias hi method is modified to private
Trait can also be combined with Trait. Trait supports abstract methods, static attributes, and static methods. the test code is as follows:
sayHello();$Obj->sayWorld();echo $Obj->getWorld() . "\n";HelloWorld::doSomething();$Obj->inc();$Obj->inc();
The output result is as follows:
HelloWorldget WorldDoing something12
Link: http://tabalt.net/blog/php-traits/