接口是对多重继承的一种变相实现, Traits可以被视为一种加强型的接口.
trait Hello { private $name = ‘chenqionghe‘; public function sayHello() { echo ‘hello ‘; } } trait World { public $age = ‘18‘; public function sayWorld() { echo ‘world ! ‘; } } class HandsonBoy { use Hello,World; public function say() { echo ‘i am ‘.$this->name; } } $boy = new HandSonBoy(); $boy->sayHello(); $boy->sayWorld(); echo $boy->say();
上面代码的运行结果如下
原文:http://www.cnblogs.com/chenqionghe/p/4735742.html