PHP中的Trait 特性及作用
自 PHP 5.4.0 起,PHP 實(shí)現(xiàn)了代碼復(fù)用的一個方法,稱為 traits。
Traits 是一種為類似 PHP 的單繼承語言而準(zhǔn)備的代碼復(fù)用機(jī)制。Trait 為了減少單繼承語言的限制,使開發(fā)人員能夠自由地在不同層次結(jié)構(gòu)內(nèi)獨(dú)立的類中復(fù)用方法集。Traits 和類組合的語義是定義了一種方式來減少復(fù)雜性,避免傳統(tǒng)多繼承和混入類(Mixin)相關(guān)的典型問題。
Trait 和一個類相似,但僅僅旨在用細(xì)粒度和一致的方式來組合功能。Trait 不能通過它自身來實(shí)例化。它為傳統(tǒng)繼承增加了水平特性的組合;也就是說,應(yīng)用類的成員不需要繼承。
Trait是在PHP5.4中加入的,它既不是接口也不是類。主要是為了解決單繼承語言的限制。是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 */ }
}
同時,如果要在 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 中聲明需要實(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 之間同時使用難免會沖突,這需要我們?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é)下注意的幾 點(diǎn):
Trait 會覆蓋調(diào)用類繼承的父類方法
Trait 無法如 Class 一樣使用 new 實(shí)例化
單個 Trait 可由多個 Trait 組成
在單個 Class 中,可以使用多個 Trait
Trait 支持修飾詞(modifiers),例如 final、static、abstract
我們能使用 insteadof 以及 as 操作符解決 Trait 之間的沖突
相關(guān)文章
詳解關(guān)于php的xdebug配置(編輯器vscode)
這篇文章主要介紹了詳解關(guān)于php的xdebug配置(編輯器vscode),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
php each 返回數(shù)組中當(dāng)前的鍵值對并將數(shù)組指針向前移動一步實(shí)例
php each函數(shù)用于獲取數(shù)組的鍵值對,并將數(shù)組指針向前移動一步, each函數(shù)經(jīng)常和list結(jié)合使用來遍歷數(shù)組。本文章向大家介紹each的基本使用方法,需要的朋友可以參考下2016-11-11
PHP設(shè)計模式之迭代器(Iterator)模式入門與應(yīng)用詳解
這篇文章主要介紹了PHP設(shè)計模式之迭代器(Iterator)模式,結(jié)合實(shí)例形式詳細(xì)分析了PHP迭代器模式的相關(guān)概念、原理、應(yīng)用案例及操作注意事項(xiàng),需要的朋友可以參考下2019-12-12
Laravel5.1 框架模型多態(tài)關(guān)聯(lián)用法實(shí)例分析
這篇文章主要介紹了Laravel5.1 框架模型多態(tài)關(guān)聯(lián)用法,結(jié)合實(shí)例形式分析了laravel5.1框架模型多態(tài)關(guān)聯(lián)具體實(shí)現(xiàn)、使用方法與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01
PHP中如何使用session實(shí)現(xiàn)保存用戶登錄信息
這篇文章主要給大家介紹在php中是如何使用session實(shí)現(xiàn)保存用戶登錄信息的,涉及到php session 用戶登錄等一些知識點(diǎn),使用session保存用戶登錄信息要比cookie安全很多。感興趣的朋友一起學(xué)習(xí)吧2015-10-10
PHP中Fatal error session_start()錯誤解決步驟
這篇文章主要介紹了PHP中Fatal error session_start()錯誤解決步驟,著重于錯誤的排除步驟,一步一步排查下去,肯定可以解決這個錯誤,需要的朋友可以參考下2014-08-08
bindParam和bindValue的區(qū)別以及在Yii2中的使用詳解
下面小編就為大家分享一篇bindParam和bindValue的區(qū)別以及在Yii2中的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
Zend Framework動作助手FlashMessenger用法詳解
這篇文章主要介紹了Zend Framework動作助手FlashMessenger用法,分析了動作助手FlashMessenger的功能,并結(jié)合實(shí)例形式演示了FlashMessenger的使用技巧,需要的朋友可以參考下2016-03-03
詳解WordPress中提醒安裝插件以及隱藏插件的功能實(shí)現(xiàn)
這篇文章主要介紹了WordPress中提醒安裝插件以及隱藏插件的功能實(shí)現(xiàn),這兩種功能通常在多用戶模式下進(jìn)行管理時用得比較多,需要的朋友可以參考下2015-12-12

