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