在整理“php基础”这篇文档时,看到了trait方法,感觉比较陌生,所以今天上午用两个小时的时间,查阅测试了trait方法的一些特性及用法,整理发布了这篇博客。
trait 是 php5.4 中的新特性,是 php 多重继承的一种解决方案。例如,需要同时继承两个 abstract class, 这将会是件很麻烦的事情,trait 就是为了解决这个问题。
trait的简单使用
trait使用前需要先定义,trait的定义方式和类的定义方式差不多
trait first_trait {
function first_method() { /* code here */ }
function second_method() { /* code here */ }
}
同时,如果要在 class 中使用该 trait,那么需要使用 use 关键字
class first_class {
// 注意这行,声明使用 first_trait
use first_trait;
}
$obj = new first_class();// executing the method from trait
$obj->first_method(); // valid
$obj->second_method(); // valid
我们在类中可以直接声明使用被定义好的trait,之后在类被实例化后,就可以使用调用对象中方法的方式直接调用trait中的方法。无疑这增加了很多扩展性,因为类的继承是单一的,而trait的使用却不是单一的。
一个class中使用多个trait
在同一个 class 中可以使用多个 trait,多个 trait之间通过逗号分隔。
例如:
trait first_trait
{
function first_method() { echo method1; }
}
trait second_trait {
function second_method() { echo method2; }
}
class first_class {
// now using more than one trait
use first_trait, second_trait;
}
$obj= new first_class();
// valid
$obj->first_method(); // print : method1
// valid
$obj->second_method(); // print : method2
trait 冲突
多个 trait 之间同时使用难免会冲突,例如,如果两个 trait 都插入了一个同名的方法,如果没有明确解决冲突将会产生一个致命错误。
为了解决多个 trait 在同一个类中的命名冲突,可以使用关键字:insteadof 以及 as ;
insteadof 操作符用来明确指定使用哪一个冲突方法;
as 操作符用来给冲突的方法重新命名并引入。
trait first_trait {
function first_function() {
echo from first trait;
}
}
trait second_trait {
// 这里的名称和 first_trait 一样,会有冲突
function first_function() {
echo from second trait;
}
}
class first_class {
use first_trait, second_trait {
// 在这里声明使用 first_trait 的 first_function 替换
// second_trait 中声明的
first_trait::first_function insteadof second_trait;
second_trait::first_function as second_trait;
}
}
trait 的抽象方法
我们可以在trait 中声明抽象方法,这样,使用它的 class 必须实现它。
trait first_trait {
function first_method() { echo method1; }
abstract public function second_method();// 这里可以加入修饰符,说明调用类必须实现它
}
class first_method {
use first_trait;
function second_method() {
/* code here */
}
}
trait优先级
php手册说法:优先顺序是当前类的方法覆盖了 trait 的方法,而 trait 覆盖了被继承的方法。
即优先级:当前类方法 > trait方法 > 被继承的方法。
如果在不同优先级中定义了一个同名的方法,那么优先级高的方法将覆盖优先级低的方法;
两个trait属于同一优先级,若出现同名方法,则会报致命错误,具体参见上面。
class base {
public function sayhello() {
echo 'hello ';
}
}
trait sayworld {
public function sayhello() {
parent::sayhello();
echo 'world!';
}
}
class myhelloworld extends base {
use sayworld;
}
$o = new myhelloworld();
$o->sayhello();
结果会输出:
hello world!
trait访问权限控制
与类相似的,我们在定义trait的方法时,也可以指定其访问权限(public、protected、private);
我们还可以使用as来修改方法的访问权限。
trait helloworld {
public function sayhello() {
echo 'hello world!';
}
}
// 修改 sayhello 的访问控制
class myclass1 {
use helloworld { sayhello as protected; }
}
// 给方法一个改变了访问控制的别名
// 原版 sayhello 的访问控制则没有发生变化
class myclass2 {
use helloworld { sayhello as private myprivatehello; }
}
trait 之间的嵌套
使用trait时,可以将不同的trait组成新的trait,这样使得代码的复用率变得更高,当然对程序的设计要求也更高。
trait hello {
public function sayhello() {
echo 'hello ';
}}
trait world {
public function sayworld() {
echo 'world!';
}}
trait helloworld {
use hello, world;
}
class myhelloworld {
use helloworld;
}
$o = new myhelloworld();
$o->sayhello();
$o->sayworld();
//输出 hello world!
trait 的静态成员
与类相似,静态成员分静态方法和静态属性,二者都是以static关键字来进行声明的。
trait counter {
public function inc() {
static $c = 0;
$c = $c + 1;
echo $c\n;
}
}
trait staticexample {
public static function dosomething() {
return 'doing something';
}
}
trait 属性
在trait中定义了属性后,类的实例可以像访问自身的属性一样访问它。而且可以给它设置访问权限,就差不能实例化了……
需要注意的是,如果 trait 定义了一个属性,那类将不能定义同样名称的属性,否则会产生一个错误。如果该属性在类中的定义与在 trait 中的定义兼容(同样的可见性和初始值)则错误的级别是 e_strict,否则是一个致命错误。
trait propertiestrait {
public $x = 1;
}
class propertiesexample {
use propertiestrait;
}
$example = new propertiesexample;
$example->x;
需要特别注意的几点
上面这些就是 trait 基本的使用了,更详细的可以参k官方手册。
这里总结下注意的几 点:
- trait 会覆盖调用类继承的父类方法
- trait 无法如 class 一样使用 new 实例化
- 单个 trait 可由多个 trait 组成
- 在单个 class 中,可以使用多个 trait
- trait 支持修饰词(modifiers),例如 final、static、abstract
- 我们能使用 insteadof 以及 as 操作符解决 trait 之间的冲突
参k文献
- http://php.net/manual/zh/language.oop5.traits.php
- https://www.doubear.com/article/raityongfaxuexibiji.html
- http://www.kuqin.com/web/20111119/315048.html
- http://www.jb51.net/article/61260.htm
最后
欢迎关注我的个人微信“华伟君”,与我一起学习探讨php,通往技术大牛的路上,我们结伴同行!