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

我们可以在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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?