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中可以实现部分方法. 下面我们以 ...
随机推荐
- nginx1.10.3+php5.6+mysql5.7.0
第一步安装nginx1.10.3 优化nginx的介绍:jemalloc https://ideas.spkcn.com/software/os/linux/577.html 预先安装autoconf ...
- 在vue-cli创建的项目里配置scss
第一步,gitbash进入到项目目录 npm install node-sass --save-dev npm install sass-loader --save-dev 第二步:打开webpack ...
- Java中WeakHashMap实现原理深究
一.前言 我发现Java很多开源框架都使用了WeakHashMap,刚开始没怎么去注意,只知道它里面存储的值会随时间的推移慢慢减少(在 WeakHashMap 中,当某个“弱键”不再正常使用时,会被从 ...
- URL大小写敏感之谜
URL其实就是我们浏览器地址栏的地址,一般由三部分组成: 协议名称,一般就是http 域名,也就是主机名 资源路径 如链接:http://www.w3school.com.cn/js/js_obj_r ...
- SQL Server “超过了锁请求超时时段”错误
错误提示:“已超过了锁请求超时时段. (Microsoft SQL Server,错误: 1222)”(英文:“Lock Request time out period exceeded.(Micro ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- 【CF MEMSQL 3.0 D. Third Month Insanity】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 分块
这个题体现了分块不只是最大值最小值众数次数,而是一种清真的思想. 我们把整个序列分块,在每个块里处理每个位置跳出这个块的次数和跳出的位置,那么每次修改n0.5,每次查询也是,那么O(m* n0.5)的 ...
- CMU Bomblab 答案
室友拉我做的... http://csapp.cs.cmu.edu/3e/labs.html Border relations with Canada have never been better. ...
- Grep basic and practice
定义:Grep (Globally search for the reqular expression and print out the line). 好处:Grep 在执行时不需要先调用编辑程序, ...