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...
Try changing your intermediate class to:
See also this comment in the docs.
Quoting from the comment:
It seems however that this will be allowed in the next PHP version 7.2:
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.
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.
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.
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()
(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
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.