OOP PHP伪多重继承 - 具有相同功能的基类

时间:2018-06-04 05:54:57

标签: php

我在PHP OOP中有点新鲜,我不知道如何调用它,但它看起来像是对我的多重继承。

class A {
    function doSomething() {
        echo "Just doing something!";
    }
}

class B extends A {
    function doSometing() {
        echo "Just do it!";
    }
}

class C extends B {
    function runDoSomething() {
        print_r($this->doSomething);
    }
}

$c = new C();
$c->runDoSomething(); // This will print class B doSomething() "Just do it!"

所以我的第一个问题是,这是一个糟糕的代码?因为当没有相同的函数时,PHP不支持多重继承和父类相互覆盖。

第二个问题,是否有另一种方法来调用类A doSomething()函数而不是在子类函数中实例化父类?

class C extends B {
    function runDoSomething() {
        $a = new A();
        print_r($a->doSomething); // This will print class A doSomething()
    }
}

0 个答案:

没有答案