PHP Fatal error: Can't inherit abstract function

后端 未结 4 1698
一生所求
一生所求 2020-12-18 22:10

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

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

abstract class CssElem extends Css {
    a         


        
相关标签:
4条回答
  • 2020-12-18 22:15

    You doesn't need to re-declare your abstract function again, declare just on implementation now.

    When you extends Css on CssElem, the function parse come together. When you implement CssElem you should implement parse function too.

    0 讨论(0)
  • 2020-12-18 22:29

    I recently encountered this Problem as well. For me it helped upgrading from PHP 5.3.8 to PHP 5.3.28.

    I don't know exactly when the change happened but in 5.3.28 it is possible to redeclare interface methods in abstract classes and still inherit from them.

    0 讨论(0)
  • 2020-12-18 22:32

    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)

    0 讨论(0)
  • 2020-12-18 22:37

    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) {}
    }
    
    0 讨论(0)
提交回复
热议问题