du0173 2017-11-24 10:44
浏览 237
已采纳

我们可以在php中创建没有抽象方法的抽象类吗? [关闭]

I want to know whether we can create an abstract class without abstract method in php or not.

If yes, then how the class is eligible to be called as 'Abstract Class' as it's not containing any abstract method? Further, is it a standard/supporting code practice?

If no, what is the reason behind it?

Can the abstract class only contain abstract methods, no more normal methods?

Note : I want the answer with some working suitable code example. The answer should be specific to PHP language only.

  • 写回答

1条回答 默认 最新

  • douzhi0107 2017-11-24 10:50
    关注

    An abstract class is a class that cannot be instantiated and must be extended by child classes. Any abstract methods in an abstract class must be implemented by an extending child class (or it must also be abstract). It's possible to have an abstract class without abstract methods to be implemented; but that's not particularly useful.

    The point of an abstract class is that you can define the interface in advance, but leave the implementation up to others. You can then work with that abstract interface and be sure it'll work as intended, without knowing in advance what the concrete implementation will be:

    abstract class Foo {
        abstract public function bar();
    }
    
    function baz(Foo $foo) {
        echo $foo->bar();
    }
    

    baz can depend on what $foo will look like exactly, without having any idea how it's implemented exactly.

    Having an abstract class without any abstract methods allows you to type hint Foo, but then you still don't know anything about what $foo will have in terms of methods or properties, so it's rather pointless.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波