dping1968 2013-07-08 11:35
浏览 46
已采纳

PHP致命错误:无法继承抽象函数

I don't understand what I'm doing wrong...

abstract class Css {
    abstract protected function parse($data);
}

abstract class CssElem extends Css {
    abstract protected function parse($data);
}

class Modifier extends CssElem {
    function __construct($data = null) {
        if( $data )
            $this->parse ($data);
    }

    protected function parse($data) {
       // Some code...
    }
}

It gives me :

[Mon Jul 8 13:21:10 2013] PHP Fatal error: Can't inherit abstract function Css::parse() (previously declared abstract in CssElem) in /home/arthur/NetBeansProjects/capa/CssElem.php on line 21 [Mon Jul 8 13:21:10 2013] 127.0.0.1:41207 [500]: / - Can't inherit abstract function Css::parse() (previously declared abstract in CssElem) in /home/arthur/NetBeansProjects/capa/CssElem.php on line 21

Line 21 is abstract protected function parse($data); in CssElem.

I'm more familiar with OOP in Java, but it seems ok according to the doc...

  • 写回答

4条回答 默认 最新

  • dongraa1986 2013-07-08 11:38
    关注

    Try changing your intermediate class to:

    abstract class CssElem extends Css {
        // abstract protected function parse($data); // <-- take this away
    }
    

    See also this comment in the docs.

    Quoting from the comment:

    An abstract class that extends an abstract class can pass the buck to its child classes when it comes to implementing the abstract methods of its parent abstract class.

    It seems however that this will be allowed in the next PHP version 7.2:

    It is now allowed to override an abstract method with another abstract method in a child class. (https://wiki.php.net/rfc/allow-abstract-function-override)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用