trait PHP 实现了一种代码复用的方法(PHP5.4.0起)

栏目: PHP · 发布时间: 7年前

内容简介:trait PHP 实现了一种代码复用的方法(PHP5.4.0起)

PHP 5.4.0 起,PHP 实现了一种代码复用的方法,称为 trait。

Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制。Trait 为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用 method。Trait 和 Class 组合的语义定义了一种减少复杂性的方式,避免传统多继承和 Mixin 类相关典型问题。 Trait 和 Class 相似,但仅仅旨在用细粒度和一致的方式来组合功能。 无法通过 trait 自身来实例化。它为传统继承增加了水平特性的组合;也就是说,应用的几个 Class 之间不需要继承。

Example #1 Trait 示例

<?php trait ezcReflectionReturnInfo {     function getReturnType() { /*1*/ }     function getReturnDescription() { /*2*/ } }  class ezcReflectionMethod extends ReflectionMethod {     use ezcReflectionReturnInfo;     /* ... */ }  class ezcReflectionFunction extends ReflectionFunction {     use ezcReflectionReturnInfo;     /* ... */ } ?>
优先级  从基类继承的成员会被 trait 插入的成员所覆盖。优先顺序是来自当前类的成员覆盖了 trait 的方法,而 trait 则覆盖了被继承的方法。

Example #2 优先顺序示例

从基类继承的成员被插入的 SayWorld Trait 中的 MyHelloWorld 方法所覆盖。其行为 MyHelloWorld 类中定义的方法一致。优先顺序是当前类中的方法会覆盖 trait 方法,而 trait 方法又覆盖了基类中的方法。
<?php 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 另一个优先级顺序的例子

<?php 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 的用法
<?php 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 的别名。
<?php 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;     } } ?>

修改方法的访问控制

<?php trait ezcReflectionReturnInfo {     function getReturnType() { /*1*/ }     function getReturnDescription() { /*2*/ } }  class ezcReflectionMethod extends ReflectionMethod {     use ezcReflectionReturnInfo;     /* ... */ }  class ezcReflectionFunction extends ReflectionFunction {     use ezcReflectionReturnInfo;     /* ... */ } ?>

0

<?php trait ezcReflectionReturnInfo {     function getReturnType() { /*1*/ }     function getReturnDescription() { /*2*/ } }  class ezcReflectionMethod extends ReflectionMethod {     use ezcReflectionReturnInfo;     /* ... */ }  class ezcReflectionFunction extends ReflectionFunction {     use ezcReflectionReturnInfo;     /* ... */ } ?>

1

从 trait 来组成 trait

<?php trait ezcReflectionReturnInfo {     function getReturnType() { /*1*/ }     function getReturnDescription() { /*2*/ } }  class ezcReflectionMethod extends ReflectionMethod {     use ezcReflectionReturnInfo;     /* ... */ }  class ezcReflectionFunction extends ReflectionFunction {     use ezcReflectionReturnInfo;     /* ... */ } ?>

2

<?php trait ezcReflectionReturnInfo {     function getReturnType() { /*1*/ }     function getReturnDescription() { /*2*/ } }  class ezcReflectionMethod extends ReflectionMethod {     use ezcReflectionReturnInfo;     /* ... */ }  class ezcReflectionFunction extends ReflectionFunction {     use ezcReflectionReturnInfo;     /* ... */ } ?>

3

Trait 的抽象成员

<?php trait ezcReflectionReturnInfo {     function getReturnType() { /*1*/ }     function getReturnDescription() { /*2*/ } }  class ezcReflectionMethod extends ReflectionMethod {     use ezcReflectionReturnInfo;     /* ... */ }  class ezcReflectionFunction extends ReflectionFunction {     use ezcReflectionReturnInfo;     /* ... */ } ?>

4

<?php trait ezcReflectionReturnInfo {     function getReturnType() { /*1*/ }     function getReturnDescription() { /*2*/ } }  class ezcReflectionMethod extends ReflectionMethod {     use ezcReflectionReturnInfo;     /* ... */ }  class ezcReflectionFunction extends ReflectionFunction {     use ezcReflectionReturnInfo;     /* ... */ } ?>

5

Trait 的静态成员

<?php trait ezcReflectionReturnInfo {     function getReturnType() { /*1*/ }     function getReturnDescription() { /*2*/ } }  class ezcReflectionMethod extends ReflectionMethod {     use ezcReflectionReturnInfo;     /* ... */ }  class ezcReflectionFunction extends ReflectionFunction {     use ezcReflectionReturnInfo;     /* ... */ } ?>

6

<? PHP

<?php trait ezcReflectionReturnInfo {     function getReturnType() { /*1*/ }     function getReturnDescription() { /*2*/ } }  class ezcReflectionMethod extends ReflectionMethod {     use ezcReflectionReturnInfo;     /* ... */ }  class ezcReflectionFunction extends ReflectionFunction {     use ezcReflectionReturnInfo;     /* ... */ } ?>

7

Example #10 静态方法

<?php trait ezcReflectionReturnInfo {     function getReturnType() { /*1*/ }     function getReturnDescription() { /*2*/ } }  class ezcReflectionMethod extends ReflectionMethod {     use ezcReflectionReturnInfo;     /* ... */ }  class ezcReflectionFunction extends ReflectionFunction {     use ezcReflectionReturnInfo;     /* ... */ } ?>

8

属性

<?php trait ezcReflectionReturnInfo {     function getReturnType() { /*1*/ }     function getReturnDescription() { /*2*/ } }  class ezcReflectionMethod extends ReflectionMethod {     use ezcReflectionReturnInfo;     /* ... */ }  class ezcReflectionFunction extends ReflectionFunction {     use ezcReflectionReturnInfo;     /* ... */ } ?>

9

优先级  从基类继承的成员会被 trait 插入的成员所覆盖。优先顺序是来自当前类的成员覆盖了 trait 的方法,而 trait 则覆盖了被继承的方法。

0

优先级  从基类继承的成员会被 trait 插入的成员所覆盖。优先顺序是来自当前类的成员覆盖了 trait 的方法,而 trait 则覆盖了被继承的方法。

1

Example #12 解决冲突

优先级  从基类继承的成员会被 trait 插入的成员所覆盖。优先顺序是来自当前类的成员覆盖了 trait 的方法,而 trait 则覆盖了被继承的方法。

2

单例

优先级  从基类继承的成员会被 trait 插入的成员所覆盖。优先顺序是来自当前类的成员覆盖了 trait 的方法,而 trait 则覆盖了被继承的方法。

3

来源:http://PHP.net/manual/zh/language.oop5.traits.PHP#language.oop5.traits.multiple


以上所述就是小编给大家介绍的《trait PHP 实现了一种代码复用的方法(PHP5.4.0起)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

SQL基础教程

SQL基础教程

MICK / 孙淼、罗勇 / 人民邮电出版社 / 2013-8-1 / CNY 69.00

本书介绍了关系数据库以及用来操作关系数据库的SQL语言的使用方法,提供了大量的示例程序和详实的操作步骤说明,读者可以亲自动手解决具体问题,循序渐进地掌握SQL的基础知识和技巧,切实提高自身的编程能力。在每章结尾备有习题,用来检验读者对该章内容的理解程度。另外本书还将重要知识点总结为“法则”,方便大家随时查阅。 本书适合完全没有或者具备较少编程和系统开发经验的初学者,也可以作为大中专院校的教材......一起来看看 《SQL基础教程》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

SHA 加密
SHA 加密

SHA 加密工具

html转js在线工具
html转js在线工具

html转js在线工具