PHP的Trait
PHP的Trait
Trait是在PHP5.4中加入的,它既不是接口也不是类。主要是为了解决单继承语言的限制。是PHP多重继承的一种解决方案。例如,需要同时继承两个 Abstract Class, 这将会是件很麻烦的事情,Trait 就是为了解决这个问题。它能被加入到一个或多个已经存在的类中。它声明了类能做什么(表明了其接口特性),同时也包含了具体实现(表明了其类特性)
简单使用
首先,当然是声明个 Trait,PHP5.4 增加了 trait 关键字
1 trait first_trait {
2 function first_method() { /* Code Here / }
3 function second_method() { / Code Here / }
4 }
同时,如果要在 Class 中使用该 Trait,那么使用 use 关键字

1 class first_class {
2 // 注意这行,声明使用 first_trait
3 use first_trait;
4 }
5
6 $obj = new first_class();
7
8 // Executing the method from trait
9 $obj->first_method(); // valid
10 $obj->second_method(); // valid

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

1 trait first_trait
2 {
3 function first_method() { echo "method1"; }
4 }
5
6 trait second_trait {
7 function second_method() { echo "method2"; }
8 }
9
10 class first_class {
11 // now using more than one trait
12 use first_trait, second_trait;
13 }
14
15 $obj= new first_class();
16
17 // Valid
18 $obj->first_method(); // Print : method1
19
20 // Valid
21 $obj->second_method(); // Print : method2

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

1 trait first_trait {
2 function first_method() { echo "method1"; }
3 }
4
5 trait second_trait {
6 use first_trait;
7 function second_method() { echo "method2"; }
8 }
9
10 class first_class {
11 // now using
12 use second_trait;
13 }
14
15 $obj= new first_class();
16
17 // Valid
18 $obj->first_method(); // Print : method1
19
20 // Valid
21 $obj->second_method(); // Print : method2

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

1 trait first_trait {
2 function first_method() { echo "method1"; }
3
4 // 这里可以加入修饰符,说明调用类必须实现它
5 abstract public function second_method();
6 }
7
8 class first_method {
9 use first_trait;
10
11 function second_method() {
12 / Code Here */
13 }
14 }

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

1 trait first_trait {
2 function first_function() {
3 echo "From First Trait";
4 }
5 }
6
7 trait second_trait {
8 // 这里的名称和 first_trait 一样,会有冲突
9 function first_function() {
10 echo "From Second Trait";
11 }
12 }
13
14 class first_class {
15 use first_trait, second_trait {
16 // 在这里声明使用 first_trait 的 first_function 替换
17 // second_trait 中声明的
18 first_trait::first_function insteadof second_trait;
19 }
20 }
21
22 $obj = new first_class();
23
24 // Output: From First Trait
25 $obj->first_function();

上面就是些 Trait 比较基本的使用了,更详细的可以参考官方手册。这里总结下注意的几 点:
• Trait 会覆盖调用类继承的父类方法
• Trait 无法如 Class 一样使用 new 实例化
• 单个 Trait 可由多个 Trait 组成
• 在单个 Class 中,可以使用多个 Trait
• Trait 支持修饰词(modifiers),例如 final、static、abstract
• 我们能使用 insteadof 以及 as 操作符解决 Trait 之间的冲突
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
更多技术文章请搜索千锋PHP,做真实的自己,用良心做教育。
互联网+时代,时刻要保持学习,携手千锋PHP,Dream It Possible。
PHP的Trait的更多相关文章
- php中trait(性状)与generator(生成器)
PHP中trait(性状)与generator(生成器) 一.trait (性状) 最近在看Josh Lockhat的<Modern PHP>,这本书很薄.但是其中给出了一个很重要的学习方 ...
- 【转】PHP的Trait 特性
Trait是在PHP5.4中加入的,它既不是接口也不是类.主要是为了解决单继承语言的限制.是PHP多重继承的一种解决方案.例如,需要同时继承两个 Abstract Class, 这将会是件很麻烦的事情 ...
- Beginning Scala study note(7) Trait
A trait provides code reusability in Scala by encapsulating method and state and then offing possibi ...
- 速战速决 (4) - PHP: 类基础, 抽象类, 接口, trait
[源码下载] 速战速决 (4) - PHP: 类基础, 抽象类, 接口, trait 作者:webabcd 介绍速战速决 之 PHP 类基础 抽象类 接口 trait 示例1.类的相关知识点 1(基础 ...
- Scala的trait
一:说明 1.介绍 2.功能 二:具体解释功能 1.定义接口 2.定义方法 3.定义字段 4.定义抽象字段 5.混合trait
- 利用GCTA工具计算复杂性状/特征(Complex Trait)的遗传相关性(genetic correlation)
如文章"Genome-wide Complex Trait Analysis(GCTA)-全基因组复杂性状分析"中介绍的GCTA,是一款基于全基因组关联分析发展的分析工具,除了计算 ...
- PHP中Trait特性
Trait是自 PHP 5.4.0 起添加的一个新特性,是 PHP 多重继承的一种解决方案.例如,需要同时继承两个 Abstract Class, 这将会是件很麻烦的事情,Trait 就是为了解决这个 ...
- scala 学习笔记(05) OOP(中)灵活的trait
trait -- 不仅仅只是接口! 接上回继续,scala是一个非常有想法的语言,从接口的设计上就可以发现它的与众不同.scala中与java的接口最接近的概念是trait,见下面的代码: packa ...
- Scala Trait
Scala Trait 大多数的时候,Scala中的trait有点类似于Java中的interface.正如同java中的class可以implement多个interface,scala中的cals ...
- scala中的trait
这里的trait字面意思是特质或者特征,这个词翻译成特征比较合适.它的意义和java,c#中接口很类似.但是trait支持部分实现,也就是说可以在scala的trait中可以实现部分方法. 下面我们以 ...
随机推荐
- GET传值
发送页: <form id="form1" runat="server"> <div> <asp:TextBox ID=</ ...
- P4462 [CQOI2018]异或序列
题目描述 已知一个长度为n的整数数列 a1,a2,...,ana_1,a_2,...,a_na1,a2,...,an ,给定查询参数l.r,问在 al,al+1,...,ara_l,a_{l+1 ...
- JavaScript词法作用域与调用对象
关于 Javascript 的函数作用域.调用对象和闭包之间的关系很微妙,关于它们的文章已经有很多,但不知道为什么很多新手都难以理解.我就尝试用比较通俗的语言来表达我自己的理解吧. 作用域 Scope ...
- Ubuntu下使用find / -name aaa* 提示“find: 路径必须在表达式之前: XXXX”
在使用find命令查找文件时,出现了如题所示的错误提示,因为之前都是这样用的,也没出过错,这次语法都是一样的居然不行了,很是纳闷. 后来了解到如下的情况: 如果当前所在目录存在要查找的目标文件时会出现 ...
- BS架构下使用消息队列的工作流程
异步通信 对于BS(Browser-Server 浏览器)架构,很多情景下server的处理时间较长. 如果浏览器发送请求后,保持跟server的连接,等待server响应,那么一方面会对用户的体验有 ...
- TypeConverter使用
如下代码, <Window.Resources> <local:Human x:Key="human" Name="Tester1" Chil ...
- is
MyType a = null; if (a is MyType) == False
- 【转载】深入理解PHP Opcode缓存原理
转载地址:深入理解PHP Opcode缓存原理 什么是opcode缓存? 当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode).O ...
- 【BZOJ1468】Tree [点分治]
Tree Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 给你一棵TREE,以及这棵树上边的距 ...
- 【BZOJ1857】【SCOI2010】传送带 [三分]
传送带 Time Limit: 1 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 在一个2维平面上有两条传送带,每一条传送 ...