PHP實(shí)現(xiàn)多繼承

方法一

自 PHP 5.4.0 起,PHP 實(shí)現(xiàn)了一種代碼復(fù)用的方法,稱為 trait。

Trait 是為類似 PHP 的單繼承語(yǔ)言而準(zhǔn)備的一種代碼復(fù)用機(jī)制。Trait 為了減少單繼承語(yǔ)言的限制,使開(kāi)發(fā)人員能夠自由地在不同層次結(jié)構(gòu)內(nèi)獨(dú)立的類中復(fù)用 method。Trait 和 Class 組合的語(yǔ)義定義了一種減少?gòu)?fù)雜性的方式,避免傳統(tǒng)多繼承和 Mixin 類相關(guān)典型問(wèn)題。

Trait 和 Class 相似,但僅僅旨在用細(xì)粒度和一致的方式來(lái)組合功能。 無(wú)法通過(guò) trait 自身來(lái)實(shí)例化。它為傳統(tǒng)繼承增加了水平特性的組合;也就是說(shuō),應(yīng)用的幾個(gè) Class 之間不需要繼承。

從基類繼承的成員會(huì)被 trait 插入的成員所覆蓋。優(yōu)先順序是來(lái)自當(dāng)前類的成員覆蓋了 trait 的方法,而 trait 則覆蓋了被繼承的方法。

trait traitTestOne{
    public function test(){
        echo "This is trait one <br/>";
    }
    public function testOne(){
        echo "one <br/>";
    }
}
 
trait traitTestTwo{
//  public function test(){
//      echo "This is trait two";
//  }
    public function testTwo(){
        echo "two <br/>";
    }
}
 
class basicTest{
    public function test(){
        echo "hello world\n";
    }
}
class myCode extends basicTest{
    use traitTestOne,traitTestTwo;
}
 
$test = new mycode();
$test->test();
$test->testOne();
$test->testTwo();

輸出:

This is trait one
one
two

方法二

class Parent1 {
    function method1() {}
    function method2() {}
}
class Parent2 {
    function method3() {}
    function method4() {}
}
class Child {
    protected $_parents = array();
    public function Child(array $parents=array()) {
        $this->_parents = $parents;
    }
     
    public function __call($method, $args) {
        // 從“父類"中查找方法
        foreach ($this->_parents as $p) {
            if (is_callable(array($p, $method))) {
                return call_user_func_array(array($p, $method), $args);
            }
        }
        // 恢復(fù)默認(rèn)的行為,會(huì)引發(fā)一個(gè)方法不存在的致命錯(cuò)誤
        return call_user_func_array(array($this, $method), $args);
    }
}
$obj = new Child(array(new Parent1(), new Parent2()));
print_r( array($obj) );die;
$obj->method1();
$obj->method3();

方法三

interface testA{   
    function echostr();   
}    
interface testB extends testA{   
    function dancing($name);   
}    
class testC implements testB{   
  
    function echostr(){   
        echo "接口繼承,要實(shí)現(xiàn)所有相關(guān)抽象方法!";   
        echo "<br>";   
    }    
  
    function dancing($name){   
        echo $name."正在跳舞!";    
    }    
}    
$demo=new testC();   
$demo->echostr();   
$demo->dancing("模特");
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言 眾所周知,一直以來(lái)PHP和很多語(yǔ)言一樣是單繼承的語(yǔ)言,但是常常在編碼過(guò)程中,我們需要在當(dāng)前類中使用兩個(gè)或兩個(gè)...
    金星show閱讀 1,947評(píng)論 0 3
  • 1、繼承單接口方式 2、繼承多接口方式 需要注意的是當(dāng)你接口繼承其它接口時(shí)候,直接繼承父接口的靜態(tài)常量屬性和抽象方...
    半畝房頂閱讀 3,554評(píng)論 0 0
  • 今天是假期的最后一天,早晨起床后,給自己做了頓早餐,強(qiáng)迫自己慢悠悠的吃了三十分鐘,只是想讓時(shí)間在慢一點(diǎn)慢一點(diǎn),慢到...
    角落里的蒙娜麗莎閱讀 185評(píng)論 2 2

友情鏈接更多精彩內(nèi)容