consider this:
class A{}
class B extends A{}
interface I{
// expects object instanceof A
function doSomething(A $a);
}
class C implements I
{
// fails ????
function doSomething(B $b){}
}
In my conception the above should work but it doesn't as php rejects that implementation requiring the first parameter to be exactly the same type (A) as defined in the interface (I). Since B is a subclass of A, I don't see whats the problem. Am I missing something here?
B
is descendent from, but not equal to,A
, and PHP's view on this is strict. It's the way it is, I don't think it's possible to work around it - you will probably just have to work without the type hint – LebensraumB
extendsA
, there's no reasonC::doSometing()
should only acceptB
, and not all objects ofA
type. – Coinclass C
definedfunction doSomething(A $a){}
instead, it should accept an argument of type B. – ShahjahanpurB
extendsA
, one should be able to useB
in each place whereA
can be used, so there's no point in limiting the type hint. – CoinI
. If the interface saysdoSomething
can be called with A, and your implementation can not called with A (any A, not only a subset of As) then your code certainly does not implement the required behavior. – Langille