PHP trait Usage Details, phptrait usage
To put it bluntly, PHP uses the trait keyword to solve the problem that a class wants to integrate the attributes and methods of the base class, and wants to have other methods of the base class, trait is usually used with use.
<?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 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?
<?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 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.
<?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 result is as follows:
Copy codeThe Code 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:
<?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; }}$Obj1 = new Class1();$Obj1->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:
Trait can also be combined with Trait. Trait supports abstract methods, static attributes, and static methods. The test code is 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::doSomething();$Obj->inc();$Obj->inc();?>
The output result is as follows:
HelloWorldget WorldDoing something12
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.