I noticed PHP behaves differently depending on whether a class is subclassing a concrete base class or implementing an abstract base class method or interface with respect to homogeny in method signatures. Consider these two examples
//----------------------------
// Strict Error
//----------------------------
class C { function blah(ArrayAccess $a) {} }
class D extends C { function blah($a) {} } // <- No type-hint in subclass method
//----------------------------
// Fatal Error
//----------------------------
interface A { function blah(ArrayAccess $a); }
class B implements A { function blah($a) {} } // <- No type-hint in subclass method
Here's the output
Strict Standards: Declaration of D::blah() should be compatible with C::blah(ArrayAccess $a) PHP Fatal error: Declaration of B::blah() must be compatible with A::blah(ArrayAccess $a)
My question - Why would PHP treat these two scenarios differently, historical reasons perhaps? Seems to me like the same problem either way.
I suppose you can get away with it in the concrete case since the subclass doesn't have to call the parent class' implementation in its implementation. Either way, I'm still curious.
When overriding methods, the parameter signature
shouldremain the same or PHP will
generate an E_STRICT level error...
And interfaces: php.net/manual/en/… ->The class implementing the interface
mustuse the exact same method signatures ... failure to do so will result in a fatal error ...
I think the thing is, that in the first case you should use the same signature and the other can't work – Rizier123