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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7269641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:22:03  来源:igfitidea点击:

php abstract class extending another abstract class

php

提问by Jakub M.

Is it possible in PHP, that an abstract class inherits from an abstract class?

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

For example,

例如,

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(); }

}

( abstract class extends abstract class in php?does not give an answer)

抽象类在php中继承了抽象类?没有给出答案)

回答by Arnaud Le Blanc

Yes, this is possible.

是的,这是可能的。

If a subclass does not implements all abstract methods of the abstract superclass, it must be abstract too.

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

回答by matino

Yes it is possible however your code would not work if you called $VeryConcreteObject->b()

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

Hereis more detailed explanation.

这里有更详细的解释。

回答by user2806167

It will work, even if you leave the abstract function b(); in class MoreConcrete.

即使你离开抽象函数 b(); 它也会起作用。在类 MoreConcrete 中。

But in this specific example I would transform class "Generic" into an Interface, as it has no more implementation beside the method definitions.

但在这个特定的例子中,我会将类“通用”转换为接口,因为它除了方法定义之外没有更多的实现。

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(); }
}