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

PHP 代码的复用简单教程

自 php 5.4.0 起,php 实现了一种代码复用的方法,称为 trait。
traits 是一种为类似 php 的单继承语言而准备的代码复用机制。trait 为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用方法集。traits 和类组合的语义是定义了一种方式来减少复杂性,避免传统多继承和混入类(mixin)相关的典型问题。
简单使用
<?phptrait test { public function echohello() { echo 'hello trait'; } }class base{ public function index() { echo 'index'; } }class one extends base{ use test; }class two extends base{ use test; } $one = new one(); $two = new two();echo $one->echohello();echo $one->index();echo $two->echohello();
结果输出 hello trait index hello trait。
从基类继承的成员会被 trait 插入的成员所覆盖。优先顺序是来自当前类的成员覆盖了 trait 的方法,而 trait 则覆盖了被继承的方法。
<?php trait test{ public function echohello() { echo 'hello trait'; }}class base { use test; public function echohello() { echo 'hello base'; }}class one extends base { use test; public function echohello() { echo 'hello one'; }}class two extends base { use test;}$one = new one();$two = new two();$base = new base(); echo $one->echohello();echo $two->echohello();echo $base->echohello(); 结果输出 hello one hello trait hello base。 ?>
class one 示例覆盖基类和 trait test,说明当前类的方法优先级高于他们。
class two 示例覆盖基类,trait 的有优先级高于继承的基类。
class base 示例覆盖 trait test,说明当前类的方法优先级高于 trait。
通过逗号分隔,在 use 声明列出多个 trait,可以都插入到一个类中。
<?phptrait test { public function echohello() { echo 'hello '; } }trait testtwo { public function echoword() { echo 'word !'; } }class one{ use test,testtwo; } $one = new one();echo $one->echohello();echo $one->echoword();
结果输出 hello word !。
如果两个 trait 都插入了一个同名的方法,如果没有明确解决冲突将会产生一个致命错误。
<?phptrait test { public function echohello() { echo 'hello test'; } public function echoword() { echo 'word test'; } }trait testtwo { public function echohello() { echo 'hello testtwo '; } public function echoword() { echo 'word testtwo'; } }class one{ use test, testtwo { test::echohello as echotest; test::echoword insteadof testtwo; testtwo::echohello insteadof test; } } $one = new one();echo $one->echotest();echo $one->echoword();echo $one->echohello();
输出结果:hello test word test hello testtwo。
使用 as 作为别名,即 test::echohello as echotest; 输出 trait test 中的 echohello.
使用 insteadof 操作符用来排除掉其他 trait,即 test::echoword insteadof testtwo; 输出的是 word test,使用 trait test 中的 echoword
修改 方法的控制权限
<?phptrait test { public function echohello() { echo 'hello'; } public function echoword() { echo 'word'; } }trait testtwo { public function echohello() { echo 'hello testtwo '; } public function echoword() { echo 'word testtwo'; } }class one{ use test { echohello as private; } }class two{ use test { echohello as private echotwo; } } $one = new one(); $two = new two();echo $two->echohello();
输出结果 hello。
class one 中使用 as 将 echohello 设为私有,则通过 class one 不能访问 echohello。
class two 中使用 as 先将其重新命名,然后将新命名方法设置为私有,原 trait 中的方法可以正常访问。
trait 中还可以像类一样定义属性。就是很好用的啦!
上面就是些 trait 比较基本的使用了,更详细的可以参考官方手册。这里总结下注意的几 点:trait 会覆盖调用类继承的父类方法,trait 无法如 class 一样使用 new 实例化
单个 trait 可由多个 trait 组成,在单个 class 中,可以使用多个 trait,trait 支持修饰词(modifiers),例如 final、static、abstract,我们能使用 insteadof 以及 as 操作符解决 trait 之间的冲突。
相关推荐:
关于trait在php中的详解及其应用
php中关于traits复用机制的使用详解
php中trait使用详解
以上就是php 代码的复用简单教程的详细内容。
其它类似信息

推荐信息