PHP中的Trait 特性及作用__第1頁
PHP中的Trait 特性及作用__第2頁
PHP中的Trait 特性及作用__第3頁
PHP中的Trait 特性及作用__第4頁
PHP中的Trait 特性及作用__第5頁
免費(fèi)預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、PHP中的Trait 特性及作用_ 自 PHP 5.4.0 起,PHP 實(shí)現(xiàn)了代碼復(fù)用的一個方法,稱為 traits。 Traits 是一種為類似 PHP 的單繼承語言而預(yù)備的代碼復(fù)用機(jī)制。Trait 為了削減單繼承語言的限制,使開發(fā)人員能夠自由地在不同層次結(jié)構(gòu)內(nèi)獨(dú)立的類中復(fù)用方法集。Traits 和類組合的語義是定義了一種方式來削減簡單性,避開傳統(tǒng)多繼承和混入類(Mixin)相關(guān)的典型問題。 Trait 和一個類相像,但僅僅旨在用細(xì)粒度和全都的方式來組合功能。Trait 不能通過它自身來實(shí)例化。它為傳統(tǒng)繼承增加了水平特性的組合;也就是說,應(yīng)用類的成員不需要繼承。 Trait是在PHP5.4中

2、加入的,它既不是接口也不是類。主要是為了解決單繼承語言的限制。是PHP多重繼承的一種解決方案。例如,需要同時繼承兩個 Abstract Class, 這將會是件很麻煩的事情,Trait 就是為了解決這個問題。它能被加入到一個或多個已經(jīng)存在的類中。它聲明白類能做什么(表明白其接口特性),同時也包含了具體實(shí)現(xiàn)(表明白其類特性) 簡潔用法 首先,當(dāng)然是聲明個 Trait,PHP5.4 增加了 trait 關(guān)鍵字 trait first_trait function first_method() /* Code Here */ function second_method() /* Code Here

3、 */ 同時,假如要在 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() ech

4、o 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 之間也可以相互的嵌套,例如

5、 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 Trai

6、t 的抽象方法(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 沖突 多個 Trait 之間同時用法難免會沖突,這需

7、要我們?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 比較基本的用法了,更具體的可以參考官方手冊。這里總結(jié)下留意的幾 點(diǎn): Trait 會掩蓋調(diào)用類繼承的父類方法 Trait 無法如 Class 一樣用法 new 實(shí)例化 單個 Trait 可由多個

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論