Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

oop - PHP Fatal error: Can't inherit abstract function

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...