【轉】PHP的Trait 特性,phptrait特性_PHP教程

來源:互聯網
上載者:User

【轉】PHP的Trait 特性,phptrait特性


Trait是在PHP5.4中加入的,它既不是介面也不是類。主要是為瞭解決單繼承語言的限制。是PHP多重繼承的一種解決方案。例如,需要同時繼承兩個 Abstract Class, 這將會是件很麻煩的事情,Trait 就是為瞭解決這個問題。它能被加入到一個或多個已經存在的類中。它聲明了類能做什麼(表明了其介面特性),同時也包含了具體實現(表明了其類特性)

簡單使用

首先,當然是聲明個 Trait,PHP5.4 增加了 trait 關鍵字

1 trait first_trait {2      function first_method() { /* Code Here */ }3      function second_method() { /* Code Here */ }4 }

同時,如果要在 Class 中使用該 Trait,那麼使用 use 關鍵字

 1 class first_class { 2     // 注意這行,聲明使用 first_trait 3     use first_trait; 4 } 5  6 $obj = new first_class(); 7  8 // Executing the method from trait 9 $obj->first_method(); // valid10 $obj->second_method(); // valid

使用多個 Trait

在同個 Class 中可以使用多個 Trait

 1 trait first_trait 2 { 3     function first_method() { echo "method1"; } 4 } 5  6 trait second_trait { 7     function second_method() { echo "method2"; } 8 } 9 10 class first_class {11     // now using more than one trait12     use first_trait, second_trait;13 }14 15 $obj= new first_class();16 17 // Valid18 $obj->first_method(); // Print : method119 20 // Valid21 $obj->second_method(); // Print : method2

Trait 之間的嵌套

同時,Trait 之間也可以相互的嵌套,例如

 1 trait first_trait { 2     function first_method() { echo "method1"; } 3 } 4  5 trait second_trait { 6     use first_trait; 7     function second_method() { echo "method2"; } 8 } 9 10 class first_class {11     // now using 12     use second_trait;13 }14 15 $obj= new first_class();16 17 // Valid18 $obj->first_method(); // Print : method119 20 // Valid21 $obj->second_method(); // Print : method2

Trait 的抽象方法(Abstract Method)

我們可以在 Trait 中聲明需要實現的抽象方法,這樣能使使用它的 Class 必須實現它

 1 trait first_trait { 2     function first_method() { echo "method1"; } 3  4     // 這裡可以加入修飾符,說明調用類必須實現它 5     abstract public function second_method(); 6 } 7  8 class first_method { 9     use first_trait;10 11     function second_method() {12         /* Code Here */13     }14 }

Trait 衝突

多個 Trait 之間同時使用難免會衝突,這需要我們去解決。PHP5.4 從文法方面帶入了相關 的關鍵字文法:insteadof 以及 as ,用法參見

 1 trait first_trait { 2     function first_function() {  3         echo "From First Trait"; 4     } 5 } 6  7 trait second_trait { 8     // 這裡的名稱和 first_trait 一樣,會有衝突 9     function first_function() { 10         echo "From Second Trait";11     }12 }13 14 class first_class {15     use first_trait, second_trait {16         // 在這裡聲明使用 first_trait 的 first_function 替換17         // second_trait 中聲明的18         first_trait::first_function insteadof second_trait;19     }20 }  21 22 $obj = new first_class();23 24 // Output: From First Trait25 $obj->first_function();

上面就是些 Trait 比較基本的使用了,更詳細的可以參考官方手冊。這裡總結下注意的幾 點:

  • Trait 會覆蓋調用類繼承的父類方法
  • Trait 無法如 Class 一樣使用 new 執行個體化
  • 單個 Trait 可由多個 Trait 組成
  • 在單個 Class 中,可以使用多個 Trait
  • Trait 支援修飾詞(modifiers),例如 final、static、abstract
  • 我們能使用 insteadof 以及 as 操作符解決 Trait 之間的衝突

文章出處:http://www.kuqin.com/web/20111119/315048.html

http://www.bkjia.com/PHPjc/1115250.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1115250.htmlTechArticle【轉】PHP的Trait 特性,phptrait特性 Trait是在PHP5.4中加入的,它既不是介面也不是類。主要是為瞭解決單繼承語言的限制。是PHP多重繼承的一...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.