I think it’ll be much easier to see the problem in a code example than writing the question in the first place. Here is my php code:
<?php
interface AnInterface
{
public function method();
}
class AClass implements AnInterface
{
public function method()
{
echo __METHOD__;
}
}
abstract class AnAbstractClass
{
abstract public function method( AnInterface $Object );
}
class ConcreteClass extends AnAbstractClass
{
public function method( AClass $Object )
{
$Object->method();
}
}
$Object1 = new ConcreteClass();
$Object2 = new AClass();
$Object1->method( $Object2 );
The above code causes the following error:
Fatal error: Declaration of ConcreteClass::method() must be compatible with that of AnAbstractClass::method()
The problem is that php doesn’t seem to be recognizing the signatures of AnAbstractClass::method and ConcreteClass::method as compatible. Am I doing something wrong? Thanks!
PHP is correct, they’re not compatible. By allowing only instances of
AClass(or its children) to be passed toConcreteClass::method, you’re breaking the contract thatAnAbstractClassprovides: Any of its subclasses must acceptAnInterfaceas an argument to itsmethod().If your example worked, and I had another class
BClassimplementingAnInterface, we’d have a situation where according toAnAbstractClass,method()should accept instances ofBClass, while according toConcreteClass, it shouldn’t.Change your signature for
ConcreteClass::methodto match that ofAnAbstractClass::method.