国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

詳解PHP中的Traits

2024-05-04 23:38:21
字體:
來源:轉載
供稿:網友

本文介紹了PHP中的Traits,Traits是一種為類似PHP的單繼承語言而準備的代碼復用機制,需要的朋友可以參考下

PHP是單繼承的語言,在PHP 5.4 Traits出現之前,PHP的類無法同時從兩個基類繼承屬性或方法。php的Traits和Go語言的組合功能類似,通過在類中使用use關鍵字聲明要組合的Trait名稱,而具體某個Trait的聲明使用trait關鍵詞,Trait不能直接實例化。具體用法請看下面的代碼:

 

 
  1. <?php 
  2. trait Drive { 
  3. public $carName = 'trait'
  4. public function driving() { 
  5. echo "driving {$this->carName}/n"
  6. class Person { 
  7. public function eat() { 
  8. echo "eat/n"
  9. class Student extends Person { 
  10. use Drive; 
  11. public function study() { 
  12. echo "study/n"
  13. $student = new Student(); 
  14. $student->study(); 
  15. $student->eat(); 
  16. $student->driving(); 

輸出結果如下:

 

 
  1. study 
  2. eat 
  3. driving trait 

上面的例子中,Student類通過繼承Person,有了eat方法,通過組合Drive,有了driving方法和屬性carName。

如果Trait、基類和本類中都存在某個同名的屬性或者方法,最終會保留哪一個呢?通過下面的代碼測試一下:

 

 
  1. <?php  
  2. trait Drive { 
  3. public function hello() { 
  4. echo "hello drive/n"
  5. public function driving() { 
  6. echo "driving from drive/n"
  7. class Person { 
  8. public function hello() { 
  9. echo "hello person/n"
  10. public function driving() { 
  11. echo "driving from person/n"
  12. class Student extends Person { 
  13. use Drive; 
  14. public function hello() { 
  15. echo "hello student/n"
  16. $student = new Student(); 
  17. $student->hello(); 
  18. $student->driving(); 

輸出結果如下:

 

 
  1. hello student 
  2. driving from drive 

因此得出結論:當方法或屬性同名時,當前類中的方法會覆蓋 trait的 方法,而 trait 的方法又覆蓋了基類中的方法。

如果要組合多個Trait,通過逗號分隔 Trait名稱:

use Trait1, Trait2;

如果多個Trait中包含同名方法或者屬性時,會怎樣呢?答案是當組合的多個Trait包含同名屬性或者方法時,需要明確聲明解決沖突,否則會產生一個致命錯誤。

 

 
  1. <?php 
  2. trait Trait1 { 
  3. public function hello() { 
  4. echo "Trait1::hello/n"
  5. public function hi() { 
  6. echo "Trait1::hi/n"
  7. trait Trait2 { 
  8. public function hello() { 
  9. echo "Trait2::hello/n"
  10. public function hi() { 
  11. echo "Trait2::hi/n"
  12. class Class1 { 
  13. use Trait1, Trait2; 

輸出結果如下:

 

  1. PHP Fatal error: Trait method hello has not been applied, because there are collisions with other trait methods on Class1 in ~/php54/trait_3.php on line 20 

使用insteadof和as操作符來解決沖突,insteadof是使用某個方法替代另一個,而as是給方法取一個別名,具體用法請看代碼:

 

 
  1. <?php 
  2. trait Trait1 { 
  3. public function hello() { 
  4. echo "Trait1::hello/n"
  5. public function hi() { 
  6. echo "Trait1::hi/n"
  7. trait Trait2 { 
  8. public function hello() { 
  9. echo "Trait2::hello/n"
  10. public function hi() { 
  11. echo "Trait2::hi/n"
  12. class Class1 { 
  13. use Trait1, Trait2 { 
  14. Trait2::hello insteadof Trait1; 
  15. Trait1::hi insteadof Trait2; 
  16. class Class2 { 
  17. use Trait1, Trait2 { 
  18. Trait2::hello insteadof Trait1; 
  19. Trait1::hi insteadof Trait2; 
  20. Trait2::hi as hei; 
  21. Trait1::hello as hehe; 
  22. $Obj1 = new Class1(); 
  23. $Obj1->hello(); 
  24. $Obj1->hi(); 
  25. echo "/n"
  26. $Obj2 = new Class2(); 
  27. $Obj2->hello(); 
  28. $Obj2->hi(); 
  29. $Obj2->hei(); 
  30. $Obj2->hehe(); 

輸出結果如下:

 

 
  1. Trait2::hello 
  2. Trait1::hi 
  3.  
  4. Trait2::hello 
  5. Trait1::hi 
  6. Trait2::hi 
  7. Trait1::hello 

as關鍵詞還有另外一個用途,那就是修改方法的訪問控制:

 

 
  1. <?php 
  2. trait Hello { 
  3. public function hello() { 
  4. echo "hello,trait/n"
  5. class Class1 { 
  6. use Hello { 
  7. hello as protected
  8. class Class2 { 
  9. use Hello { 
  10. Hello::hello as private hi; 
  11. $Obj1 = new Class1(); 
  12. $Obj1->hello(); # 報致命錯誤,因為hello方法被修改成受保護的 
  13. $Obj2 = new Class2(); 
  14. $Obj2->hello(); # 原來的hello方法仍然是公共的 
  15. $Obj2->hi(); # 報致命錯誤,因為別名hi方法被修改成私有的 

Trait 也能組合Trait,Trait中支持抽象方法、靜態屬性及靜態方法,測試代碼如下:

 

 
  1. <?php 
  2. trait Hello { 
  3. public function sayHello() { 
  4. echo "Hello/n"
  5. trait World { 
  6. use Hello; 
  7. public function sayWorld() { 
  8. echo "World/n"
  9. abstract public function getWorld(); 
  10. public function inc() { 
  11. static $c = 0; 
  12. $c = $c + 1; 
  13. echo "$c/n"
  14. public static function doSomething() { 
  15. echo "Doing something/n"
  16. class HelloWorld { 
  17. use World; 
  18. public function getWorld() { 
  19. return 'get World'
  20. $Obj = new HelloWorld(); 
  21. $Obj->sayHello(); 
  22. $Obj->sayWorld(); 
  23. echo $Obj->getWorld() . "/n"
  24. HelloWorld::doSomething(); 
  25. $Obj->inc(); 
  26. $Obj->inc(); 

輸出結果如下:

 

 
  1. Hello 
  2. World 
  3. get World 
  4. Doing something 

以上就是本文的全部內容,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 饶平县| 莎车县| 安顺市| 石门县| 高安市| 淳化县| 拜城县| 鄂伦春自治旗| 邵阳市| 通渭县| 景宁| 青龙| 江川县| 定南县| 兰坪| 台前县| 石台县| 张家川| 栖霞市| 永城市| 巴马| 金山区| 北辰区| 灵石县| 克山县| 壤塘县| 买车| 五家渠市| 中西区| 拉孜县| 潞西市| 紫金县| 高州市| 会理县| 柳州市| 精河县| 巴楚县| 桦甸市| 石柱| 吉木萨尔县| 盘山县|