Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 379
Arthur Avatar asked Jul 08 '13 11:07

Arthur


2 Answers

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)

like image 117
Matteo Tassinari Avatar answered Sep 19 '22 20:09

Matteo Tassinari


Patrick Huy's answer was definitely helpful.

This bug is documented here:
https://bugs.php.net/bug.php?id=43200
As noted there, the bug is relevant if you want to extend legacy interfaces.

It can be reproduced on http://3v4l.org, showing that it is fixed since PHP 5.3.9.

Simplest form

This can be "fixed" with the solution by Matteo Tassinari, by commenting out the definition of foo() in the second interface.

http://3v4l.org/qo6sG
5.0.0 - 5.3.8: Fatal error: Can't inherit abstract function A::foo()
5.3.9 - *: No problem.

interface A {
  function foo();
}

interface B extends A {
  function foo();
}

With added optional parameters

http://3v4l.org/ZXq0O
5.0.0 - 5.3.8: Fatal error: Can't inherit abstract function A::foo()
5.3.9 - *: No problem.

interface A {
  function foo();
}

interface B extends A {
  function foo($x = NULL);
}

With added required parameters

http://3v4l.org/5fPBO
5.0.0 - 5.3.8: Fatal error: Can't inherit abstract function A::foo()
5.3.9 - *: Fatal error: Declaration of B::foo() must be compatible with (that of) A::foo()

interface A {
  function foo();
}

interface B extends A {
  function foo($x);
}

(We are not surprised, this breaks in all PHP versions)

With added optional parameters in implementation:

http://3v4l.org/UvcLA
5.0.0 - *: No problem

/**
 * @legacy
 */
interface A {
    function foo();
}

/**
 * Supersedes legacy interface A
 */
interface B extends A {
    /**
     * @param int|null $x
     *   Optional parameter added in new version.
     */
    # function foo($x = NULL);
}

class C implements B {
    function foo($x = NULL) {}
}
like image 35
donquixote Avatar answered Sep 17 '22 20:09

donquixote