Trait 是什麼東西

PHP官方手冊裏面寫的內容是php

自 PHP 5.4.0 起,PHP 實現了一種代碼複用的方法,稱爲 trait。oop

Trait 是爲相似 PHP 的單繼承語言而準備的一種代碼複用機制。Trait 爲了減小單繼承語言的限制,使開發人員可以自由地在不一樣層次結構內獨立的類中複用 method。Trait 和 Class 組合的語義定義了一種減小複雜性的方式,避免傳統多繼承和 Mixin 類相關典型問題。this

Trait 和 Class 類似,但僅僅旨在用細粒度和一致的方式來組合功能。 沒法經過 trait 自身來實例化。它爲傳統繼承增長了水平特性的組合;也就是說,應用的幾個 Class 之間不須要繼承。spa

<?php
trait Trait_a {
    function getReturnType() { /*1*/ }
    function getReturnDescription() { /*2*/ }
}

trait Trait_b {
    function getReturnType() { /*1*/ }
    function getReturnDescription() { /*2*/ }
}

class class1 extends parent_class1 {
    use Trait_a;
    /* ... */
}

class class2 extends parent_class2 {
    use Trait_a,Trait_b;
    /* ... */
}

 

使用use語句利用這個特性。經過逗號分隔,在 use 聲明列出多個 trait,能夠都插入到一個類中。.net

從基類繼承的成員會被 trait 插入的成員所覆蓋。優先順序是來自當前類的成員覆蓋了 trait 的方法,而 trait 則覆蓋了被繼承的方法。code

也就是 當前類 > trait > 父類blog

若是兩個 trait 都插入了一個同名的方法,若是沒有明確解決衝突將會產生一個致命錯誤。繼承

爲了解決多個 trait 在同一個類中的命名衝突,須要使用 insteadof 操做符來明確指定使用衝突方法中的哪個。ip

以上方式僅容許排除掉其它方法,as 操做符能夠 爲某個方法引入別名。 注意,as 操做符不會對方法進行重命名,也不會影響其方法。開發

衝突的解決

在本例中 Talker 使用了 trait A 和 B。因爲 A 和 B 有衝突的方法,其定義了使用 trait B 中的 smallTalk 以及 trait A 中的 bigTalk。

Aliased_Talker 使用了 as 操做符來定義了 talk 來做爲 B 的 bigTalk 的別名。

<?php
trait A {
    public function smallTalk() {
        echo 'a';
    }
    public function bigTalk() {
        echo 'A';
    }
}

trait B {
    public function smallTalk() {
        echo 'b';
    }
    public function bigTalk() {
        echo 'B';
    }
}

class Talker {
    use A, B {
        B::smallTalk insteadof A;
        A::bigTalk insteadof B;
    }
}

class Aliased_Talker {
    use A, B {
        B::smallTalk insteadof A;
        A::bigTalk insteadof B;
        B::bigTalk as talk;
    }
}
?>

 

Note:

在 PHP 7.0 以前,在類裏定義和 trait 同名的屬性,哪怕是徹底兼容的也會拋出 E_STRICT(徹底兼容的意思:具備相同的訪問可見性、初始默認值)。

修改方法的訪問控制

使用 as 語法還能夠用來調整方法的訪問控制。

修改方法的訪問控制

<?php
trait HelloWorld {
    public function sayHello() {
        echo 'Hello World!';
    }
}

// 修改 sayHello 的訪問控制
class MyClass1 {
    use HelloWorld { sayHello as protected; }
}

// 給方法一個改變了訪問控制的別名
// 原版 sayHello 的訪問控制則沒有發生變化
class MyClass2 {
    use HelloWorld { sayHello as private myPrivateHello; }
}
?>

 

 

從 trait 來組成 trait

正如 class 可以使用 trait 同樣,其它 trait 也可以使用 trait。在 trait 定義時經過使用一個或多個 trait,可以組合其它 trait 中的部分或所有成員。

從 trait 來組成 trait

<?php
trait Hello {
    public function sayHello() {
        echo 'Hello ';
    }
}

trait World {
    public function sayWorld() {
        echo 'World!';
    }
}

trait HelloWorld {
    use Hello, World;
}

class MyHelloWorld {
    use HelloWorld;
}

$o = new MyHelloWorld();
$o->sayHello();
$o->sayWorld();
?>

 

以上例程會輸出:

Hello World!

Trait 的抽象成員

爲了對使用的類施增強制要求,trait 支持抽象方法的使用。

 表示經過抽象方法來進行強制要求

<?php
trait Hello {
    public function sayHelloWorld() {
        echo 'Hello'.$this->getWorld();
    }
    abstract public function getWorld();
}

class MyHelloWorld {
    private $world;
    use Hello;
    public function getWorld() {
        return $this->world;
    }
    public function setWorld($val) {
        $this->world = $val;
    }
}
?>

 

Trait 的靜態成員

Traits 能夠被靜態成員靜態方法定義。

Example 靜態變量

<?php
trait Counter {
    public function inc() {
        static $c = 0;
        $c = $c + 1;
        echo "$c\n";
    }
}

class C1 {
    use Counter;
}

class C2 {
    use Counter;
}

$o = new C1(); $o->inc(); // echo 1
$p = new C2(); $p->inc(); // echo 1
?>

 

Example  靜態方法

<?php
trait StaticExample {
    public static function doSomething() {
        return 'Doing something';
    }
}

class Example {
    use StaticExample;
}

Example::doSomething();
?>

 

Trait 一樣能夠定義屬性。

Example  定義屬性

<?php
trait PropertiesTrait {
    public $x = 1;
}

class PropertiesExample {
    use PropertiesTrait;
}

$example = new PropertiesExample;
$example->x;
?>

 

Trait 定義了一個屬性後,類就不能定義一樣名稱的屬性,不然會產生 fatal error。 有種狀況例外:屬性是兼容的(一樣的訪問可見度、初始默認值)。 在 PHP 7.0 以前,屬性是兼容的,則會有 E_STRICT 的提醒。

Example  解決衝突

<?php
trait PropertiesTrait {
    public $same = true;
    public $different = false;
}

class PropertiesExample {
    use PropertiesTrait;
    public $same = true; // PHP 7.0.0 後沒問題,以前版本是 E_STRICT 提醒
    public $different = true; // 致命錯誤
}
?>
相關文章
相關標籤/搜索