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 "method1"; }
} trait second_trait {
function second_method() { echo "method2"; }
} class first_class {
// now using more than one trait
use first_trait, second_trait;
} $obj= new first_class(); // Valid
$obj->first_method(); // Print : method1 // Valid
$obj->second_method(); // Print : method2

Trait 之间的嵌套

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

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

Trait 的抽象方法(Abstract Method)

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

 trait first_trait {
function first_method() { echo "method1"; } // 这里可以加入修饰符,说明调用类必须实现它
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 之间的冲突
注意:
1、本博客同步更新到我的个人网站:http://www.zhaoyafei.cn
2、本文非原创内容,为了尊重他人劳动,转载请到文章原文地址:
http://www.kuqin.com/web/20111119/315048.html
 

【转】PHP的Trait 特性的更多相关文章

  1. PHP中Trait特性

    Trait是自 PHP 5.4.0 起添加的一个新特性,是 PHP 多重继承的一种解决方案.例如,需要同时继承两个 Abstract Class, 这将会是件很麻烦的事情,Trait 就是为了解决这个 ...

  2. trait特性

    1.trait特性可以和特化或者偏特化结合. 2.trait可以和类型转换结合.

  3. PHP Trait特性

    php类的单继承性,无法同时从两个基类中继承属性和方法,为了解决这个问题,使用Trait特性解决. Trait是一种代码复用技术,为PHP的单继承限制提供了一套灵活的代码复用机制. 用法:通过在类中使 ...

  4. php类知识---trait特性

    #由于php类只支持单一继承,但我们又需要使用一些类的优秀特性,因此有了trait <?php trait cpc #trait 下的方法只能用public { function trainni ...

  5. PHP trait 特性在 Laravel 中的使用个人心得

    trait 是在PHP5.4中为了方便代码复用的一种实现方式,但目前我在看的的PHP项目中较少看的有程序员去主动使用这个实现方式,在laravel中有很多 trait 的使用,关于trait 在 la ...

  6. 【Scala篇】--Scala中Trait、模式匹配、样例类、Actor模型

    一.前述 Scala Trait(特征) 相当于 Java 的接口,实际上它比接口还功能强大. 模式匹配机制相当于java中的switch-case. 使用了case关键字的类定义就是样例类(case ...

  7. Laravel中Trait的用法实例详解

    本文实例讲述了Laravel中Trait的用法.分享给大家供大家参考,具体如下: 看看PHP官方手册对Trait的定义: 自 PHP 5.4.0 起,PHP 实现了代码复用的一个方法,称为 trait ...

  8. scala当中的特质trait

    1.将trait作为接口使用 Scala中的trait是一种特殊的概念: 首先先将trait作为接口使用,此时的trait就与Java中的接口 (interface)非常类似: 在trait中可以定义 ...

  9. php5.4.0以后加入trait实现代码复用【摘录】

    在http://www.php.net/manual/zh/language.oop5.traits.php 查看了一下5.4.0提供的特性trait顺便做一个标记(总结): 首先要明确trait和类 ...

随机推荐

  1. Markdown与标记语言

    Markdown 是一种轻量级的「标记语言」,它的优点很多,目前也被越来越多的写作爱好者,撰稿者广泛使用.看到这里请不要被「标记」.「语言」所迷惑,Markdown 的语法十分简单.常用的标记符号也不 ...

  2. 面向对象继承 (for in 原型链查找属性)

    window.onload=function(){ new Preson('liujian','男').show(); new Work('liujian','男','工人').show(); new ...

  3. JestClient

    JestService.java [html] view plain copy 在CODE上查看代码片派生到我的代码片 public class JestService { /** * 获取JestC ...

  4. 自己动手编写spring IOC源码

    前言:对于spring IOC概念不是很了解的朋友可以阅读我上一篇博客--轻松理解spring IOC(这两篇博客也是由于我的个人原因导致现在才发布,惭愧啊).通过这篇博客的理解之后,相信大家会对sp ...

  5. python 生成器等语法

    生成器 调用生成器函数,不会执行生成器函数中的代码,而是返回一个对象,  这个对象是生成器(可用type()函数判断这个对象类型),  如果要运行生成器函数中的代码, 需要调用 next()方法,   ...

  6. iOS 10、Xcode 8 遇到部分问题解决记录

    今天把iphone 6 升级到ios10 后,用Xcode 7进行真机调试的时候提示: Could not find Developer Disk Image 果断准备升级到Xcode 8 .但是想保 ...

  7. 美团HD(4)-二级联动效果

    DJNavDropView.m #import "DJNavDropView.h" #import "DJCategory.h" #import "D ...

  8. C# 反射研究

    概念 反射这东西,对于我这种小白,听起来总是觉得好大上的. 当初理解它费了一点时间,后来看了一句话,突然恍然大悟,“反射就跟B超一样,我们在不剖开人体的情况下想看清楚内部情况, 我们就通过发射超声波, ...

  9. java中数组的基本知识

    数组{ 物以类聚 人以群分 avg sum 数组 数组的概念[ 数组:一组具有相同数据类型的集合 ] 数组的语法[ 先声明 后使用 声明数组: 数据类型+[]+ 变量名 ;/ 数据类型+变量名 +[] ...

  10. Android常用组件之ListView

    使用ListAdapter是ListView的适配器. 使用ListView.setAdapter()方法来设置ListView的适配器. 简单的使用 adapter = new ArrayAdapt ...