自 php 5.4.0 起,php 实现了代码复用的一个方法,称为 traits。
traits 是一种为类似 php 的单继承语言而准备的代码复用机制。trait 为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用方法集。traits 和类组合的语义是定义了一种方式来减少复杂性,避免传统多继承和混入类(mixin)相关的典型问题。
traits 和一个类相似,但仅仅旨在用细粒度和一致的方式来组合功能。trait 不能通过它自身来实例化。它为传统继承增加了水平特性的组合;也就是说,应用类的成员不需要继承。
example #1 trait 示例
trait ezcreflectionreturninfo{
function getreturntype(){}
function getreturndescription(){}
}
class ezcreflectionmethod extends reflectionmethod{
use ezcreflectionreturninfo;
}
class ezcreflectionfunction extends reflectionfunction{
use ezcreflectionreturninfo;
}
优先级
从基类继承的成员被 trait 插入的成员所覆盖。优先顺序是来自当前类的成员覆盖了 trait 的方法,而 trait 则覆盖了被继承的方法。
example #2 优先顺序示例
从基类继承的成员被插入的 sayworld trait 中的 myhelloworld 方法所覆盖。其行为 myhelloworld 类中定义的方法一致。优先顺序是当前类中的方法会覆盖 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!
example #3 另一个优先级顺序的例子
trait helloworld{
public function sayhello(){
echo 'hello world!';
}
}
class theworldisnotenough{
use helloworld;
public function sayhello(){
echo 'hello universe!';
}
}
$o = new theworldisnotenough;
$o -> sayhello();
输出结果:
hello universe!
多个 trait
通过逗号分隔,在 use 声明列出多个 trait,可以都插入到一个类中。
example #4 多个 trait 的用法
trait hello{
public function sayhello(){
echo 'hello ';
}
}
trait world{
public function sayworld(){
echo 'world';
}
}
class myhelloworld{
use hello,world;
public function sayexclamationmark(){
echo '!';
}
}
$o = new myhelloworld();
$o -> sayhello();
$o -> sayworld();
$o -> sayexclamationmark();
输出结果:
hello world!
冲突的解决
如果两个 trait 都插入了一个同名的方法,如果没有明确解决冲突将会产生一个致命错误。
为了解决多个 trait 在同一个类中的命名冲突,需要使用 insteadof 操作符来明确指定使用冲突方法中的哪一个。
以上方式仅允许排除掉其它方法,as 操作符可以将其中一个冲突的方法以另一个名称来引入。
example #5 冲突的解决
在本例中 talker 使用了 trait a 和 b。由于 a 和 b 有冲突的方法,其定义了使用 trait b 中的 smalltalk 以及 trait a 中的 bigtalk。
aliased_talker 使用了 as 操作符来定义了 talk 来作为 b 的 bigtalk 的别名。
trait a{
public function smalltalk(){
echo 'a';
}
public function bigtalk(){
echo 'a';
}
}
trait b{
public function smalltalk(){
echo 'b';
}
public function bigtalk(){
echo 'b';
}
}
class talker{
use a,b{
b::smalltalk insteadof a;
a::bigtalk insteadof b;
}
}
class aliased_talker{
use a,b{
b::smalltalk insteadof a;
a::bigtalk insteadof b;
b::bigtalk as talk;
}
}
$t = new talker;
$t->smalltalk(); //b
$t->bigtalk(); //a
$at = new aliased_talker;
$at->smalltalk(); //b
$at->bigtalk(); //a
$at->talk(); //b
修改方法的访问控制
使用 as 语法还可以用来调整方法的访问控制。
example #6 修改方法的访问控制
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。在 trait 定义时通过使用一个或多个 trait,它能够组合其它 trait 中的部分或全部成员。
example #7 从 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 的抽象成员
为了对使用的类施加强制要求,trait 支持抽象方法的使用。
example #8 表示通过抽象方法来进行强制要求
trait hello{
public function sayhelloworld(){
echo 'hello'.$this->getworld();
}
abstract public function getworld();
}
class myhelloworld{
private $world;
use hello;
public function getworld(){
return $this->world;
}
public function setworld($val){
$this->world = $val;
}
}
trait 的静态成员
静态变量可以被 trait 的方法引用,但不能被 trait 定义。但是 trait 能够为使用的类定义静态方法。
example #9 静态变量
trait counter{
public function inc(){
static $c = 0;
$c = $c + 1;
echo "{$c}<br>";
}
}
class c1{
use counter;
}
class c2{
use counter;
}
$o = new c1();
$o->inc(); //echo 1
$p = new c2;
$p->inc(); //echo 1
example #10 静态方法
trait staticexample{
public static function dosomething(){
return 'doing something.';
}
}
class example{
use staticexample;
}
example::dosomething();
输出结果:doing something.
属性
trait 同样可以定义属性。
example #11 定义属性
trait propertiestrait{
public $x = 1;
}
class propertiesexample{
use propertiestrait;
}
$example = new propertiesexample;
$example->x;
如果 trait 定义了一个属性,那类将不能定义同样名称的属性,否则会产生一个错误。如果该属性在类中的定义与在 trait 中的定义兼容(同样的可见性和初始值)则错误的级别是 e_strict,否则是一个致命错误。
example #12 冲突
trait propertiestrait{
public $sname = true;
public $different = false;
}
class propertiesexample{
use propertiestrait;
public $sname = true; //strict standards
public $different = true; //致命错误
}