PHP抽象类扩展另一个抽象类


php abstract class extending another abstract class

在PHP中,抽象类从抽象类继承是可能的吗?

例如

abstract class Generic {
    abstract public function a();
    abstract public function b();
}
abstract class MoreConcrete extends Generic {
    public function a() { do_stuff(); }
    abstract public function b(); // I want this not to be implemented here...
}
class VeryConcrete extends MoreConcrete {
    public function b() { do_stuff(); }
}

(抽象类扩展抽象类在php?没有给出答案)

这是可能的。

如果子类没有实现抽象超类的所有抽象方法,那么它也必须是抽象的。

它将工作,即使你离开抽象函数b();MoreConcrete.

但是在这个特定的例子中,我将把类"Generic"转换为一个接口,因为除了方法定义之外,它没有更多的实现。

interface Generic {
    public function a(); 
    public function b();
}
abstract class MoreConcrete implements Generic {
    public function a() { do_stuff(); }
    // can be left out, as the class is defined abstract
    // abstract public function b();
}
class VeryConcrete extends MoreConcrete {
    // this class has to implement the method b() as it is not abstract.
    public function b() { do_stuff(); }
}

是的,这是可能的,但是你的代码将无法工作,如果你调用$VeryConcreteObject->b()