PHP中的Trait 特性及作用


Posted in PHP onApril 03, 2016

自 PHP 5.4.0 起,PHP 实现了代码复用的一个方法,称为 traits。

Traits 是一种为类似 PHP 的单继承语言而准备的代码复用机制。Trait 为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用方法集。Traits 和类组合的语义是定义了一种方式来减少复杂性,避免传统多继承和混入类(Mixin)相关的典型问题。

Trait 和一个类相似,但仅仅旨在用细粒度和一致的方式来组合功能。Trait 不能通过它自身来实例化。它为传统继承增加了水平特性的组合;也就是说,应用类的成员不需要继承。

Trait是在PHP5.4中加入的,它既不是接口也不是类。主要是为了解决单继承语言的限制。是PHP多重继承的一种解决方案。例如,需要同时继承两个 Abstract Class, 这将会是件很麻烦的事情,Trait 就是为了解决这个问题。它能被加入到一个或多个已经存在的类中。它声明了类能做什么(表明了其接口特性),同时也包含了具体实现(表明了其类特性)

简单使用

首先,当然是声明个 Trait,PHP5.4 增加了 trait 关键字

trait first_trait {
function first_method() { /* Code Here */ }
function second_method() { /* Code Here */ }
}

同时,如果要在 Class 中使用该 Trait,那么使用 use 关键字

class first_class {
// 注意这行,声明使用 first_trait
use first_trait;
}
$obj = new first_class();
// Executing the method from trait
$obj->first_method(); // valid
$obj->second_method(); // valid

使用多个 Trait

在同个 Class 中可以使用多个 Trait

trait first_trait
{
function first_method() { echo "method"; }
}
trait second_trait {
function second_method() { echo "method"; }
}
class first_class {
// now using more than one trait
use first_trait, second_trait;
}
$obj= new first_class();
// Valid
$obj->first_method(); // Print : method
// Valid
$obj->second_method(); // Print : method

Trait 之间的嵌套

同时,Trait 之间也可以相互的嵌套,例如

trait first_trait {
function first_method() { echo "method"; }
}
trait second_trait {
use first_trait;
function second_method() { echo "method"; }
}
class first_class {
// now using 
use second_trait;
}
$obj= new first_class();
// Valid
$obj->first_method(); // Print : method
// Valid
$obj->second_method(); // Print : method

Trait 的抽象方法(Abstract Method)

我们可以在 Trait 中声明需要实现的抽象方法,这样能使使用它的 Class 必须实现它

trait first_trait {
function first_method() { echo "method"; }
// 这里可以加入修饰符,说明调用类必须实现它
abstract public function second_method();
}
class first_method {
use first_trait;
function second_method() {
/* Code Here */
}
}

Trait 冲突

多个 Trait 之间同时使用难免会冲突,这需要我们去解决。PHP5.4 从语法方面带入了相关 的关键字语法:insteadof 以及 as ,用法参见

trait first_trait {
function first_function() { 
echo "From First Trait";
}
}
trait second_trait {
// 这里的名称和 first_trait 一样,会有冲突
function first_function() { 
echo "From Second Trait";
}
}
class first_class {
use first_trait, second_trait {
// 在这里声明使用 first_trait 的 first_function 替换
// second_trait 中声明的
first_trait::first_function insteadof second_trait;
}
} 
$obj = new first_class();
// Output: From First Trait
$obj->first_function();

上面就是些 Trait 比较基本的使用了,更详细的可以参考官方手册。这里总结下注意的几 点:

Trait 会覆盖调用类继承的父类方法

Trait 无法如 Class 一样使用 new 实例化

单个 Trait 可由多个 Trait 组成

在单个 Class 中,可以使用多个 Trait

Trait 支持修饰词(modifiers),例如 final、static、abstract

我们能使用 insteadof 以及 as 操作符解决 Trait 之间的冲突

PHP 相关文章推荐
PHP 模板高级篇总结
Dec 21 PHP
php一句话cmdshell新型 (非一句话木马)
Apr 18 PHP
PHP Undefined index报错的修复方法
Jul 17 PHP
SESSION信息保存在哪个文件目录下以及能够用来保存什么类型的数据
Jun 17 PHP
php实现Mongodb自定义方式生成自增ID的方法
Mar 23 PHP
PHP.ini安全配置检测工具pcc简单介绍
Jul 02 PHP
php投票系统之增加与删除投票(管理员篇)
Jul 01 PHP
PHP 获取指定地区的天气实例代码
Feb 08 PHP
基于win2003虚拟机中apache服务器的访问
Aug 01 PHP
PHP5.5新特性之yield理解与用法实例分析
Jan 11 PHP
PHP的mysqli_stat()函数讲解
Jan 23 PHP
tp5.1 框架查询表达式用法详解
May 25 PHP
PHP错误处理函数
Apr 03 #PHP
php通过curl添加cookie伪造登陆抓取数据的方法
Apr 02 #PHP
PHP函数nl2br()与自定义函数nl2p()换行用法分析
Apr 02 #PHP
PHP使用stream_context_create()模拟POST/GET请求的方法
Apr 02 #PHP
PHP函数shuffle()取数组若干个随机元素的方法分析
Apr 02 #PHP
PHP函数import_request_variables()用法分析
Apr 02 #PHP
PHP数组函数array_multisort()用法实例分析
Apr 02 #PHP
You might like
Apache2 httpd.conf 中文版
2006/11/17 PHP
安装PHP可能遇到的问题“无法载入mysql扩展” 的解决方法
2007/04/16 PHP
php检测图片主要颜色的方法
2015/07/01 PHP
php模仿qq空间或朋友圈发布动态、评论动态、回复评论、删除动态或评论的功能(中)
2017/06/11 PHP
PHP中abstract(抽象)、final(最终)和static(静态)原理与用法详解
2020/06/05 PHP
JS 文件传参及处理技巧分析
2010/05/13 Javascript
javascript的创建多行字符串的7种方法
2014/04/29 Javascript
简单对比分析JavaScript中的apply,call与this的使用
2015/12/04 Javascript
js实现select下拉框菜单
2015/12/08 Javascript
JS使用JSON作为参数实例分析
2016/06/23 Javascript
Angular2 环境配置详细介绍
2016/09/21 Javascript
JS中substring与substr的用法
2016/11/16 Javascript
代码详解JS操作剪贴板
2018/02/11 Javascript
angularjs下ng-repeat点击元素改变样式的实现方法
2018/09/12 Javascript
javascript中的event loop事件循环详解
2018/12/14 Javascript
简单了解vue.js数组的常用操作
2019/06/17 Javascript
jQuery表单校验插件validator使用方法详解
2020/02/18 jQuery
JavaScript实现轮播图特效
2020/04/10 Javascript
jQuery实现移动端扭蛋机抽奖
2020/11/08 jQuery
树莓派使用USB摄像头和motion实现监控
2019/06/22 Python
Python获取一个用户名的组ID过程解析
2019/09/03 Python
Django使用消息提示简单的弹出个对话框实例
2019/11/15 Python
基于selenium及python实现下拉选项定位select
2020/07/22 Python
解决margin 外边距合并问题
2019/07/03 HTML / CSS
Canvas 像素处理之改变透明度的实现代码
2019/01/08 HTML / CSS
如何获得EntityManager
2014/02/09 面试题
秘书英文求职信范文
2014/01/31 职场文书
残疾人创业典型事迹
2014/02/01 职场文书
校庆标语集锦
2014/06/25 职场文书
八达岭长城导游词
2015/01/30 职场文书
加薪申请报告范本
2015/05/15 职场文书
thinkphp 获取控制器及控制器方法
2021/04/16 PHP
用基于python的appium爬取b站直播消费记录
2021/04/17 Python
Python中的变量与常量
2021/11/11 Python
MySQL表锁、行锁、排它锁及共享锁的使用详解
2022/04/02 MySQL
MySQL中优化SQL语句的方法(show status、explain分析服务器状态信息)
2022/04/09 MySQL