不废话,上代码:
/* * @purpose php 5.4 traits test */ trait Foo { public $name, $age, $height; public function aaa() { echo 'aaa' . $this->name; } } trait bar { public function getAge() { echo $this->age; } } class test { use Foo, bar; public function __construct($name, $age, $height) { $this->name = $name; $this->age = $age; $this->height = $height; } public function sayHi() { echo 'Hi!'; } } $obj = new test('suxiaolin', 20, 180); echo $obj->aaa(); echo '<br />'; echo $obj->getAge();
traits功能远不止这些,欲了解详情,请上官网查看。