PHP 5.4中的traits特性
生活随笔
收集整理的這篇文章主要介紹了
PHP 5.4中的traits特性
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Trait 是 PHP5.4 中的新特性,是 PHP 多重繼承的一種解決方案。例如,需要同時繼承兩個 Abstract Class, 這將會是件很麻煩的事情,Trait 就是為了解決這個問題。簡單使用
首先,當(dāng)然是聲明個 Trait,PHP5.4 增加了 trait 關(guān)鍵字trait first_trait {function first_method() { /* Code Here */ }function second_method() { /* Code Here */ }
}
同時,如果要在 Class 中使用該 Trait,那么使用 use 關(guān)鍵字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 中可以使用多個 Traittrait 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 中聲明需要實(shí)現(xiàn)的抽象方法,這樣能使使用它的 Class 必須實(shí)現(xiàn)它trait first_trait {function first_method() { echo "method1"; }// 這里可以加入修飾符,說明調(diào)用類必須實(shí)現(xiàn)它abstract public function second_method();
}class first_method {use first_trait;function second_method() {/* Code Here */}
}
Trait 沖突
多個 Trait 之間同時使用難免會沖突,這需要我們?nèi)ソ鉀Q。PHP5.4 從語法方面帶入了相關(guān) 的關(guān)鍵字語法: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();
需要注意的幾點(diǎn)
上面就是些 Trait 比較基本的使用了,更詳細(xì)的可以參考官方手冊。這里總結(jié)下注意的幾 點(diǎn):Trait 會覆蓋調(diào)用類繼承的父類方法
Trait 無法如 Class 一樣使用 new 實(shí)例化
單個 Trait 可由多個 Trait 組成
在單個 Class 中,可以使用多個 Trait
Trait 支持修飾詞(modifiers),例如 final、static、abstract
我們能使用 insteadof 以及 as 操作符解決 Trait 之間的沖突
-- Split --一些看法
坦白講,我第一眼看到 Trait 對它并沒有任何好感。PHP5 以來帶來的新特性已經(jīng)足夠得 多,而且讓開發(fā)者們有點(diǎn)應(yīng)接不暇。同時,Trait 更像是程序員的“語法糖”,然而它提供便利的同時可能會造成巨大的隱患。 例如 Trait 能夠調(diào)用類中的成員: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;}
}
同時,針對類中已經(jīng)實(shí)現(xiàn)的方法,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!
?
轉(zhuǎn)載于:https://www.cnblogs.com/qhorse/p/4762517.html
總結(jié)
以上是生活随笔為你收集整理的PHP 5.4中的traits特性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LINUX2.4.x网络安全框架
- 下一篇: 小蚂蚁学习Redis笔记(13)——Re