源碼地址:https://github.com/wilfordw/phpTutorial
該系列我只寫我的理解,非官方解釋,如不夠專業(yè)請見諒
PHP是單繼承的語言,在PHP 5.4 Traits
出現(xiàn)之前,PHP的類無法同時從兩個基類繼承屬性或方法。
PHP5.4以后有了Traits
,才解決了這一問題。
通過在類中使用use
關鍵字聲明要組合的Trait名稱,而具體某個Trait的聲明使用trait
關鍵詞,Trait不能直接實例化,Trait的存在更類似于接口和抽象類
以下是循序漸進給出幾個trait的實踐
<?php
//trait 與 繼承
trait Drive {
public $carName = 'trait';
public function driving() {
echo "driving {$this->carName}\n";
}
}
class Person {
public function eat() {
echo "eat\n";
}
}
class Student extends Person {
use Drive;
public function study() {
echo "study\n";
}
}
$student = new Student();
$student->study();//study
$student->eat();//eat
$student->driving();//driving trait
上面的例子中,Student類通過繼承Person,有了eat方法,通過組合Drive,有了driving方法和屬性carName
<?php
//同名屬性或方法 當前類覆蓋trait trait覆蓋基類
trait Drive {
public function hello() {
echo "hello drive\n";
}
public function driving() {
echo "driving from drive\n";
}
}
class Person {
public function hello() {
echo "hello person\n";
}
public function driving() {
echo "driving from person\n";
}
}
class Student extends Person {
use Drive;
public function hello() {
echo "hello student\n";
}
}
$student = new Student();
$student->hello();//hello student
$student->driving();//driving from drive
當方法或屬性同名時,當前類中的方法會覆蓋 trait的 方法,而 trait 的方法又覆蓋了基類中的方法。
<?php
//多個Trait包含同名屬性或者方法
trait Trait1 {
public function hello() {
echo "Trait1::hello\n";
}
public function hi() {
echo "Trait1::hi\n";
}
}
trait Trait2 {
public function hello() {
echo "Trait2::hello\n";
}
public function hi() {
echo "Trait2::hi\n";
}
}
class Class1 {
use Trait1, Trait2;
}
//產(chǎn)生致命錯誤
//Fatal error: Trait method hello has not been applied, because there are collisions with other trait methods on Class1 in /Users/wilford/Sites/phpTutorial/traits/example3.php on line 19
當組合的多個Trait包含同名屬性或者方法時,需要明確聲明解決沖突,否則會產(chǎn)生一個致命錯誤。
<?php
trait Trait1 {
public function hello() {
echo "Trait1::hello\n";
}
public function hi() {
echo "Trait1::hi\n";
}
}
trait Trait2 {
public function hello() {
echo "Trait2::hello\n";
}
public function hi() {
echo "Trait2::hi\n";
}
}
class Class1 {
use Trait1, Trait2 {
Trait2::hello insteadof Trait1;//用insteadof解決沖突
Trait1::hi insteadof Trait2;
}
}
class Class2 {
use Trait1, Trait2 {
Trait2::hello insteadof Trait1;
Trait1::hi insteadof Trait2;
Trait2::hi as hei;//用as修改別名
Trait1::hello as hehe;
}
}
$Obj1 = new Class1();
$Obj1->hello();//Trait2::hello
$Obj1->hi();//Trait1::hi
echo "\n";
$Obj2 = new Class2();
$Obj2->hello();//Trait2::hello
$Obj2->hi();//Trait1::hi
$Obj2->hei();//Trait2::hi
$Obj2->hehe();//Trait1::hello
使用
insteadof
和as
操作符來解決沖突,insteadof
是使用某個方法替代另一個,而as
是給方法取一個別名
<?php
trait Hello {
public function hello() {
echo "hello,trait\n";
}
}
class Class1 {
use Hello {
hello as protected; //as修改方法訪問權限
}
}
class Class2 {
use Hello {
Hello::hello as private hi;
}
}
$Obj1 = new Class1();
// $Obj1->hello(); # 報致命錯誤,因為hello方法被修改成受保護的
$Obj2 = new Class2();
$Obj2->hello(); # 原來的hello方法仍然是公共的
//$Obj2->hi(); # 報致命錯誤,因為別名hi方法被修改成私有的
as
關鍵詞還有另外一個用途,那就是修改方法的訪問控制:
<?php
trait Hello {
public function sayHello() {
echo "Hello\n";
}
}
trait World {
use Hello;
public function sayWorld() {
echo "World\n";
}
abstract public function getWorld();//抽象方法
public function inc() {
static $c = 0;//靜態(tài)變量
$c = $c + 1;
echo "$c\n";
}
public static function doSomething() {//靜態(tài)方法
echo "Doing something\n";
}
}
class HelloWorld {
use World;
public function getWorld() {
return 'get World';
}
}
$Obj = new HelloWorld();
$Obj->sayHello();#Hello
$Obj->sayWorld();#World
echo $Obj->getWorld() . "\n";#get World
HelloWorld::doSomething();#Doing something
$Obj->inc();#1
$Obj->inc();#2
Trait 也能組合Trait,Trait中支持抽象方法、靜態(tài)屬性及靜態(tài)方法
到此已經(jīng)把Traits的用法介紹的差不多了。Traits是一個很重要的特征,很多大型框架都在用比如Laveral,需要熟練掌握