Reputation: 3797
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...
Upvotes: 14
Views: 11426
Reputation: 18584
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)
Upvotes: 16
Reputation: 5471
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.
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();
}
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);
}
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)
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) {}
}
Upvotes: 12
Reputation: 995
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.
Upvotes: 3
Reputation: 2790
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.
Upvotes: 2