PHP 5.4中的traits特性
Trait 是 PHP5. 中的新特性,是 PHP 多重继承的一种解决方案。例如,需要同时继承两个 Abstract Class, 这将会是件很麻烦的事情,Trait 就是为了解决这个问题。 简单使用
首先,当然是声明个 Trait,PHP5. 增加了 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. 从语法方面带入了相关 的关键字语法: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 之间的冲突
-- Split -- 一些看法
坦白讲,我第一眼看到 Trait 对它并没有任何好感。PHP5 以来带来的新特性已经足够得 多,而且让开发者们有点应接不暇。 同时,Trait 更像是程序员的“语法糖”,然而它提供便利的同时可能会造成巨大的隐患。 例如 Trait 能够调用类中的成员: trait Hello {
public function sayHelloWorld() {
echo 'Hello'.$this->getWorld();
}
abstract public function getWorld();
} class MyHelloWorld {
private $world;
use Hello;
public function getWorld() {
return $this->world;
}
public function setWorld($val) {
$this->world = $val;
}
}
同时,针对类中已经实现的方法,Trait 没有效果 trait HelloWorld {
public function sayHello() {
echo 'Hello World!';
}
} class TheWorldIsNotEnough {
use HelloWorld;
public function sayHello() {
echo 'Hello Universe!';
}
} $o = new TheWorldIsNotEnough();
$o->sayHello(); // echos Hello Universe!
PHP 5.4中的traits特性的更多相关文章
- 转:PHP 5.4中的traits
原文来自于:http://www.cnblogs.com/thinksasa/archive/2013/05/16/3081247.html PHP 5.4中的traits,是新引入的特性,中文还真不 ...
- PHP 5.4 中的traits
PHP 5.4中的traits,是新引入的特性,中文还真不知道如何准确翻译好.其实际的目的,是为了有的场合想用多继承,但PHP又没多继承,于是就发明了这样的一个东西. Traits (横向重用/多重继 ...
- 如何在 PHP 和 Laravel 中使用 Traits
事实上,PHP 作为一门编程语言存在的问题之一,就是你只能使用单继承.这意味着一个类只能从另一个类中继承.例如,可能希望从几个不同的类继承方法,以防止代码重复.在 PHP 5.4 中 一个新的语言特性 ...
- Java语言中的面向对象特性总结
Java语言中的面向对象特性 (总结得不错) [课前思考] 1. 什么是对象?什么是类?什么是包?什么是接口?什么是内部类? 2. 面向对象编程的特性有哪三个?它们各自又有哪些特性? 3. 你知 ...
- ASP.NET 5与MVC 6中的新特性
差点忘了提一句,MVC 6中默认的渲染引擎Razor也将得到更新,以支持C# 6中的新语法.而Razor中的新特性还不只这一点. 在某些情况下,直接在Web页面中嵌入某些JSON数据的方式可能比向服务 ...
- ASP.NET MVC4中的bundles特性引发服务器拒绝访问(403错误)
在ASP.NET MVC4中微软引入了bundles特性,这个特性可以将服务器端的多个Javascript或多个css文件捆绑在一起作为一个单一的URL地址供客户端浏览器调用,从而减少了页面上Http ...
- 关于C# 中的Attribute 特性
关于C# 中的Attribute 特性 作者: 钢钢 来源: 博客园 发布时间: 2011-01-09 23:30 阅读: 13921 次 推荐: 12 原文链接 [收藏] 摘要:纠结地说 ...
- 专家解读Linux操作系统内核中的GCC特性
专家解读Linux操作系统内核中的GCC特性 Linux内核使用GNU Compiler Collection (GCC)套件的几个特殊功能.这些功能包括提供快捷方式和简化以及向编译器提供优化提示 ...
- OC中的一个特性:延展
OC中的一个特性:延展其实说白了,延展就是弥补C语言中的前向申明,我们知道,在C语言中,如果你想调用一个函数的话,那么在此之前必须要声明一个这个函数,就是有前置性.OC中为了弥补C语言中的这个问题,就 ...
随机推荐
- 操作haproxy配置文件教师版
作用: 可查,可增,可删,可修改 #_*_coding:utf-8_*_ import os def file_handle(filename,backend_data,record_list=Non ...
- serialVersionUID要注意以下几点:
今天在使用eclipse开发的时候,遇到一个warning,看到warning我总觉得不爽,使用自动修复后,发现eclipse在代码中加入了“private static final long ser ...
- delphi基本语法
本文参考自<delphi2010语法手册> 1. 工程文件结构 源文件联系着unit单元,delphi主模块源文件格式为.dpr,其他模块为.pas,一个完整程序由一个.dpr和若干.pa ...
- sublime Text 3实用功能和常用快捷键收集
下面是我通过网上视频教程或文本资料学习sublime Text3时收集的一些实用功能和常用快捷键,现在分享出来,如果还有其它的好用的功能可以在下面留言,以便互相学习. PS:ST3在Mac OX与Wi ...
- 支付宝微信O2O大战,WiFi广告在夹缝中求生存
支付宝微信O2O大战,WiFi广告在夹缝中求生存 来自工信部的数据显示,截至2013年底,中国智能手机的保有量已经达到5.8亿台.国内平均有46%的时间选择WiFi上网. 商用WiFi已经成为了移动互 ...
- Protocol Buffers介绍
基本概念 Protocol Buffers(以下简称PB)是一种独立于语言.独立于开发平台.可扩展的序列化数据结构框架,它常常被用在通信.数据序列化保存等方面. PB是一种敏捷.高效.自动化的用于对数 ...
- java笔记--策略模式和简单工厂模式
策略模式: --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3884781.html "谢谢-- 为什么使用:策略模式主要用于 ...
- [Effective JavaScript 笔记]第33条:使构造函数与new操作符无关
当使用函数作为一个构造函数时,程序依赖于调用者是否记得使用new操作符来调用该构造函数.注意:该函数假设接收者是一个全新的对象. 一个例子 function User(name,pwd){ this. ...
- Window_搭建SVN服务器
http://wenku.baidu.com/link?url=614FLi_VlhiJpyG5bq8JcwFBHroMjsV3FvBDzyyn0snZ85jbWx7xh-RPJdH7stxlgM9i ...
- mac安装最新版本的git
文章转载自我的个人博客 http://www.iwangzheng.com/ 1.下载一个安装包哦 在mac下安装git,首先下载一个git安装包,这里我选的是git-1.9.3.tar , 因为这是 ...