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 Static关键字实用方法
Jun 04 PHP
PHP函数之日期时间函数date()使用详解
Sep 09 PHP
php stream_get_meta_data返回值
Sep 29 PHP
phpmyadmin打开很慢的解决方法
Apr 21 PHP
PHP FTP操作类代码( 上传、拷贝、移动、删除文件/创建目录)
May 10 PHP
PHP调用wsdl文件类型的接口代码分享
Nov 19 PHP
PHP 使用redis简单示例分享
Mar 05 PHP
Zend Framework教程之Zend_Db_Table表关联实例详解
Mar 23 PHP
ThinkPHP模板循环输出Volist标签用法实例详解
Mar 23 PHP
浅谈php数组array_change_key_case() 函数和array_chunk()函数
Oct 22 PHP
ubutu 16.04环境下,PHP与mysql数据库,网页登录验证实例讲解
Jul 20 PHP
tp5.1框架数据库子查询操作实例分析
May 26 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
再次研究下cache_lite
2007/02/14 PHP
PHP实现把文本中的URL转换为链接的auolink()函数分享
2014/07/29 PHP
PHP开发中解决并发问题的几种实现方法分析
2017/11/13 PHP
ext 代码生成器
2009/08/07 Javascript
js监听输入框值的即时变化onpropertychange、oninput
2011/07/13 Javascript
jquery利用event.which方法获取键盘输入值的代码
2011/10/09 Javascript
javascript 另一种图片滚动切换效果思路
2012/04/20 Javascript
JQuery设置文本框和密码框得到焦点时的样式
2013/08/30 Javascript
jQuery实现新消息闪烁标题提示的方法
2015/03/11 Javascript
javascript数组随机排序实例分析
2015/07/22 Javascript
javascript中Date format(js日期格式化)方法小结
2015/12/17 Javascript
基于JQuery的$.ajax方法进行异步请求导致页面闪烁的解决办法
2016/05/10 Javascript
JS基于递归实现倒计时效果的方法
2016/11/26 Javascript
jQuery Validate让普通按钮触发表单验证的方法
2016/12/15 Javascript
JS编写函数实现对身份证号码最后一位的验证功能
2016/12/29 Javascript
详解从Node.js的child_process模块来学习父子进程之间的通信
2017/03/27 Javascript
详解vue.js之绑定class和style的示例代码
2017/08/24 Javascript
ES6学习教程之模板字符串详解
2017/10/09 Javascript
Vuex 入门教程
2018/01/10 Javascript
vue 根据数组中某一项的值进行排序的方法
2018/08/30 Javascript
Vue自定义指令上报Google Analytics事件统计的方法
2019/02/25 Javascript
详解vue 不同环境配置不同的打包命令
2019/04/07 Javascript
js实现一款简单踩白块小游戏(曾经很火)
2019/12/02 Javascript
浅谈Python中数据解析
2015/05/05 Python
python实现生成字符串大小写字母和数字的各种组合
2019/01/01 Python
在python里协程使用同步锁Lock的实例
2019/02/19 Python
用python打印菱形的实操方法和代码
2019/06/25 Python
Pycharm快捷键配置详细整理
2020/10/13 Python
移动端html5模拟长按事件的实现方法
2018/09/30 HTML / CSS
全球知名鞋履品牌授权零售商:Journeys
2016/09/17 全球购物
澳大利亚在线购买儿童玩具:Toy Universe
2017/12/28 全球购物
正隆泰信息技术有限公司上机题
2012/06/14 面试题
连锁酒店店长职责范本
2014/02/13 职场文书
母亲节感恩活动记录
2014/03/16 职场文书
2019商业计划书格式、范文
2019/04/24 职场文书
MySql统计函数COUNT的具体使用详解
2022/08/14 MySQL