PHP中的Trait 特性及作用
自 PHP 5.4.0 起,PHP 實現(xiàn)了代碼復(fù)用的一個方法,稱為 traits。
Traits 是一種為類似 PHP 的單繼承語言而準(zhǔn)備的代碼復(fù)用機(jī)制。Trait 為了減少單繼承語言的限制,使開發(fā)人員能夠自由地在不同層次結(jié)構(gòu)內(nèi)獨立的類中復(fù)用方法集。Traits 和類組合的語義是定義了一種方式來減少復(fù)雜性,避免傳統(tǒng)多繼承和混入類(Mixin)相關(guān)的典型問題。
Trait 和一個類相似,但僅僅旨在用細(xì)粒度和一致的方式來組合功能。Trait 不能通過它自身來實例化。它為傳統(tǒng)繼承增加了水平特性的組合;也就是說,應(yīng)用類的成員不需要繼承。
Trait是在PHP5.4中加入的,它既不是接口也不是類。主要是為了解決單繼承語言的限制。是PHP多重繼承的一種解決方案。例如,需要同時繼承兩個 Abstract Class, 這將會是件很麻煩的事情,Trait 就是為了解決這個問題。它能被加入到一個或多個已經(jīng)存在的類中。它聲明了類能做什么(表明了其接口特性),同時也包含了具體實現(xiàn)(表明了其類特性)
簡單使用
首先,當(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 中可以使用多個 Trait
trait first_trait { function first_method() { echo "method"; } } trait second_trait { function second_method() { echo "method"; } } class first_class { // now using more than one trait use first_trait, second_trait; } $obj= new first_class(); // Valid $obj->first_method(); // Print : method // Valid $obj->second_method(); // Print : method
Trait 之間的嵌套
同時,Trait 之間也可以相互的嵌套,例如
trait first_trait { function first_method() { echo "method"; } } trait second_trait { use first_trait; function second_method() { echo "method"; } } class first_class { // now using use second_trait; } $obj= new first_class(); // Valid $obj->first_method(); // Print : method // Valid $obj->second_method(); // Print : method
Trait 的抽象方法(Abstract Method)
我們可以在 Trait 中聲明需要實現(xiàn)的抽象方法,這樣能使使用它的 Class 必須實現(xiàn)它
trait first_trait { function first_method() { echo "method"; } // 這里可以加入修飾符,說明調(diào)用類必須實現(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();
上面就是些 Trait 比較基本的使用了,更詳細(xì)的可以參考官方手冊。這里總結(jié)下注意的幾 點:
Trait 會覆蓋調(diào)用類繼承的父類方法
Trait 無法如 Class 一樣使用 new 實例化
單個 Trait 可由多個 Trait 組成
在單個 Class 中,可以使用多個 Trait
Trait 支持修飾詞(modifiers),例如 final、static、abstract
我們能使用 insteadof 以及 as 操作符解決 Trait 之間的沖突
相關(guān)文章
淺談php serialize()與unserialize()的用法
本篇文章是對php中的serialize()與unserialize()的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06thinkPHP3.0框架實現(xiàn)模板保存到數(shù)據(jù)庫的方法
這篇文章主要介紹了thinkPHP3.0框架實現(xiàn)模板保存到數(shù)據(jù)庫的方法,結(jié)合實例形式分析了使用thinkPHP3.0框架開發(fā)CMS系統(tǒng)過程中將模板保存到數(shù)據(jù)庫的具體實現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08laravel框架關(guān)于搜索功能的實現(xiàn)
本文是作者整理的關(guān)于laravel框架搜索功能的實現(xiàn)原理,并附上了詳細(xì)代碼,有需要的小伙伴請持續(xù)關(guān)注!2018-03-03php+ajax實現(xiàn)無刷新動態(tài)加載數(shù)據(jù)技術(shù)
無刷新功能我們用到很多很多的,下面我就來給各位介紹一個實例,就是實現(xiàn)php+ajax實現(xiàn)無刷新滾屏加載數(shù)據(jù),例子非常的簡單大家只要按流程來操作就可以了哦。2015-04-04php發(fā)送get、post請求的6種方法簡明總結(jié)
這篇文章主要介紹了php發(fā)送get、post請求的6種方法簡明總結(jié),分別為使用file_get_contents 、fopen、fsockopen、curl來發(fā)送GET和POST請求,需要的朋友可以參考下2014-07-07