PHP 5.4 的 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
在同个 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 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 中声明需要实现的抽象方法,这样能使使用它的 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 之间同时使用难免会冲突,这需要我们去解决。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 之间的冲突
坦白讲,我第一眼看到 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!
那么 Trait 的出现是为何呢?有哥们的回答比较有意思,但不无道理:
因为php没有javascript作用域链的机制,所以无法把function bind到class里面,曾经以为php 5.3的闭包可以做这个事,最后才发觉作用域的设计不允许这么干
但话说回来,拿 interface 和 Trait 类比,显然 Trait 有更多方便的地方(虽然 两者不能完全相互替代)。
不过很显然 Trait 目前还处于测试阶段,它的未来相比其他 PHP5 新推来的特性还有 更多让人观望的地方,但或许这特性能改变 PHP5 未来继承的方式。
因为,我个人坚信 PHP 的作用链设计迟早会改得“更像 JavaScript”,即便这事情会在遥远的 PHP6 。
参考链接 http://php.net/manual/en/language.oop5.traits.php https://wiki.php.net/rfc/traits http://en.wikipedia.org/wiki/Trait_%28computer_programming%29 http://bbs.phpchina.com/thread-210870-1-1.html http://scg.unibe.ch/research/traits/ http://walu.sinaapp.com/?p=60 http://www.phppan.com/2011/07/mixin-and-trait/转自: http://www.kuqin.com/web/20111119/315048.html

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

长URL(通常用关键字和跟踪参数都混乱)可以阻止访问者。 URL缩短脚本提供了解决方案,创建了简洁的链接,非常适合社交媒体和其他平台。 这些脚本对于单个网站很有价值

在Facebook在2012年通过Facebook备受瞩目的收购之后,Instagram采用了两套API供第三方使用。这些是Instagram Graph API和Instagram Basic Display API。作为开发人员建立一个需要信息的应用程序

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

这是有关用Laravel后端构建React应用程序的系列的第二个也是最后一部分。在该系列的第一部分中,我们使用Laravel为基本的产品上市应用程序创建了一个RESTFUL API。在本教程中,我们将成为开发人员

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

2025年的PHP景观调查调查了当前的PHP发展趋势。 它探讨了框架用法,部署方法和挑战,旨在为开发人员和企业提供见解。 该调查预计现代PHP Versio的增长
