PHP Trait 的基本概念与组合使用
通俗而言,PHP 引入 trait 关键字旨在突破单一继承的限制,使类在继承父类属性和方法的同时,能够复用其他类的功能。通常情况下,trait 需配合 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();
?>
执行上述代码后,控制台输出结果如下:
study
eat
driving trait
在上述示例中,Student 类通过继承 Person 类获得了 eat 方法,并通过 use 组合 Drive trait,从而拥有了 driving 方法及 carName 属性。
若 Trait、基类与本类中均定义了同名的属性或方法,系统最终会保留哪一个定义呢?
<?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();
?>
运行后的输出结果如下:
hello student
driving from drive
由此可得出结论:当出现同名方法或属性时,当前类中的定义优先级最高,会覆盖 Trait 中的定义;而 Trait 中的定义又会覆盖基类中的定义。
若需组合多个 Trait,只需在 use 语句中以逗号分隔各个 Trait 的名称即可:
use Trait1, Trait2;
如果多个被组合的 Trait 中包含同名方法或属性,将会发生什么?答案是必须明确声明解决冲突的方案,否则程序将抛出致命错误。
<?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";
}
}
class Class1 {
use Trait1, Trait2;
}
?>
未处理冲突时的输出结果如下:
复制代码 代码如下: 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
可以使用 insteadof 和 as 操作符来解决此类冲突。insteadof 用于指定使用某个特定方法替代另一个,而 as 则用于为方法创建别名。具体用法参见以下代码:
<?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";
}
}
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();
?>
修正冲突后的输出结果如下:
Trait2::hello
Trait1::hi
Trait2::hello
Trait1::hi
Trait2::hi
Trait1::hello
as 关键词还有另一项用途,即修改方法的访问控制权限:
Trait 之间也可以相互组合。此外,Trait 内部支持定义抽象方法、静态属性以及静态方法,测试代码如下:
<?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 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();
?>
相关测试的输出结果如下:
Hello
World
get World
Doing something
1
2
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。