您好,欢迎访问一九零五行业门户网

PHP中Trait的解析

这篇文章主要介绍了关于php中trait的解析,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
自 php 5.4.0 起,php 实现了一种代码复用的方法,称为 trait。
trait其字面意思是”特性”、”特点”,我们可以理解为,使用trait关键字,可以为php中的类添加新的特性。
trait 是为类似 php 的单继承语言而准备的一种代码复用机制。trait 为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用 method。trait 和 class 组合的语义定义了一种减少复杂性的方式,避免传统多继承和 mixin 类相关典型问题。
trait 和 class 相似,但仅仅旨在用细粒度和一致的方式来组合功能。 无法通过 trait 自身来实例化。它为传统继承增加了水平特性的组合;也就是说,应用的几个 class 之间不需要继承。
用法:通过在类中使用use 关键字,声明要组合的trait名称,具体的trait的声明使用trait关键词,trait不能实例化。
1、traits基础
<?phpheader("content-type:text/html;charset=utf-8");trait test{public function hello1(){ return "test::hello1()"; }}class demo1{ use test;}$obj = new demo1();echo $obj->hello1().'<br />';//test::hello1()
2、优先级
<?phpclass 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!
<?phptrait helloworld { public function sayhello() { echo 'hello world!'; }}class theworldisnotenough { use helloworld; public function sayhello() { echo 'hello universe!'; }}$o = new theworldisnotenough();$o->sayhello();//hello universe!
从基类继承的成员被插入的 sayworld trait 中的 myhelloworld 方法所覆盖。其行为 myhelloworld 类中定义的方法一致。优先顺序是当前类中的方法会覆盖 trait 方法,而 trait 方法又覆盖了基类中的方法。
3、多个traits
通过逗号分隔,在 use 声明列出多个 trait,可以都插入到一个类中。
<?phptrait hello { public function sayhello() { echo 'hello '.'<br />'; }}trait world { public function sayworld() { echo 'world'.'<br />'; }}class myhelloworld { use hello, world; public function sayexclamationmark() { echo '!'.'<br />'; }}$o = new myhelloworld();$o->sayhello();//hello$o->sayworld();//world$o->sayexclamationmark();//!
4、冲突的解决
如果两个 trait 都插入了一个同名的方法,如果没有明确解决冲突将会产生一个致命错误。
为了解决多个 trait 在同一个类中的命名冲突,需要使用 insteadof 操作符来明确指定使用冲突方法中的哪一个。
<?phptrait 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; }}$obj = new talker();$obj->smalltalk();//b$obj->bigtalk();//a
以上方式仅允许排除掉其它方法,as 操作符可以 为某个方法引入别名。 注意,as 操作符不会对方法进行重命名,也不会影响其方法。
<?phptrait 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; b::bigtalk as b_bigtalk; a::smalltalk as a_smalltalk; }}$obj = new talker();$obj->smalltalk();//b$obj->bigtalk();//a$obj->b_bigtalk();//b$obj->a_smalltalk();//a
5、修改方法的访问控制
使用 as 语法还可以用来调整方法的访问控制。
<?phptrait a{ private function smalltalk(){ echo 'a'; }}class talker{ use a{ smalltalk as public aaa; }}$obj = new talker();$obj->aaa();//a
6、traits组
正如 class 能够使用 trait 一样,其它 trait 也能够使用 trait。在 trait 定义时通过使用一个或多个 trait,能够组合其它 trait 中的部分或全部成员。
<?phptrait hello{ public function sayhello(){ echo 'hello '; }}trait world{ public function sayworld(){ echo 'world !'; }}trait helloworld{ use hello,world;}class talker{ use helloworld;}$obj = new talker();$obj->sayhello();//hello$obj->sayworld();//world !
7、抽象成员
为了对使用的类施加强制要求,trait 支持抽象方法的使用。
<?phptrait hello{ public function sayworld(){ echo 'hello '.$this->getworld(); } abstract public function getworld();}class talker{ private $world; use hello; public function getworld(){ return $this->world; } public function setworld($val){ $this->world = $val; }}$obj = new talker();$obj->setworld("trait !");$obj->sayworld();//hello trait !
8、traits静态成员
traits 可以被静态成员静态方法定义。
<?phptrait helloworld{ public static function sayhelloworld(){ echo 'hello world !'; }}class talker{ use helloworld;}talker::sayhelloworld();//hello world !
<?phptrait counter{ public function inc(){ static $c = 0; $c++; echo "$c\n"; }}class c1{ use counter;}class c2{ use counter;}$c1 = new c1();$c1->inc();//1$c1_1 = new c1();$c1_1->inc();//2$c2 = new c2();$c2->inc();//1
9、属性
trait 同样可以定义属性。
<?phptrait propertiestrait { public $x = 1;}class propertiesexample { use propertiestrait;}$example = new propertiesexample;echo $example->x;//1
trait 定义了一个属性后,类就不能定义同样名称的属性,否则会产生 fatal error。 有种情况例外:属性是兼容的(同样的访问可见度、初始默认值)。 在 php 7.0 之前,属性是兼容的,则会有 e_strict 的提醒。
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
php中使用elasticsearch的方法
php定时器的说明
以上就是php中trait的解析的详细内容。
其它类似信息

推荐信息