Summary of basic usage examples of traits in php

伊谢尔伦
Release: 2023-03-12 13:42:01
Original
1170 people have browsed it

To put it simply, the trait keyword is used in PHP to solve the problem that a class wants to integrate the attributes and methods of a base class, but also wants to have other base classes. method, and trait is generally used in conjunction with use.

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();

?>
Copy after login

The output results are as follows:

study
eat
driving trait
Copy after login

In the above example, the Student class inherits Person through , has the eat method, and through the combination of Drive, there is driving Methods and properties carName.

If there is a property or method with the same name in Trait, base class and this class, which one will be retained in the end?

hello();
  $student->driving();
?>
Copy after login

The output results are as follows:

hello student
driving from drive
Copy after login

So we can conclude that when a method or attribute has the same name, the method in the current class will override the trait's method, and the trait's method will override the base class. method in.

If you want to combine multiple Traits, separate the Trait names by commas:

use Trait1, Trait2;
Copy after login

What happens if multiple Traits contain methods or properties with the same name? The answer is that when multiple combined Traits contain properties or methods with the same name, they need to be explicitly declared to resolve conflicts, otherwise a fatal error will occur.

Copy after login

The output results are as follows:

The 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
Copy after login

Use insteadof and asoperators to resolve conflicts, insteadof is to use a method instead Another one, and as is to give an alias to the method. Please see the code for specific usage:

hello();
$Obj1->hi();
echo "\n";
$Obj2 = new Class2();
$Obj2->hello();
$Obj2->hi();
$Obj2->hei();
$Obj2->hehe();
?>
Copy after login

The output result is as follows:

Trait2::hello
Trait1::hi

Trait2::hello
Trait1::hi
Trait2::hi
Trait1::hello
Copy after login

The as keyword has another use, which is to modify the method. Access Control:

Trait can also be combined with Trait. Trait supports abstract methods, static properties and static methods. The test code is as follows:

sayHello();
$Obj->sayWorld();
echo $Obj->getWorld() . "\n";
HelloWorld::doSomething();
$Obj->inc();
$Obj->inc();
?>
Copy after login

The output is as follows:

Hello
World
get World
Doing something
1
2
Copy after login

The above is the detailed content of Summary of basic usage examples of traits in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!