您的位置:首页 > 编程语言 > PHP开发

php新特性 traits 简单方法复用

2013-04-09 22:05 549 查看
不废话,上代码:

/*
* @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功能远不止这些,欲了解详情,请上官网查看。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: