In short I have
abstract class AbstractMapper implements MapperInterface {
public function fetch(EntityInterface $entity, Array $conditions = array()) {
. . .
}
}
interface MapperInterface {
public function fetch(EntityInterface $entity, Array $conditions = array());
}
abstract class AbstractUserMapper extends AbstractMapper implements UserMapperInterface {
public function fetch(UserInterface $user, Array $conditions = array()) {
$conditions = array_merge($conditions, array('type' => $user->getType()));
return parent::fetch($user, $conditions);
}
}
interface UserMapperInterface {
public function fetch(UserInterface $user, Array $conditions = array());
}
This is the error I get:
Fatal error: Declaration of Model\Data\Mappers\AbstractUserMapper::fetch() must be compatible with that of Model\Data\Mappers\Interfaces\MapperInterface::fetch()
If I change the UserInterface
to EntityInterface
it works but it just seems wrong and also in my AbstractUserMapper::fetch()
when I type$user
my IDE shows only the methods declared in my EntityInterface
and getType()
is not in that list.
I know I can still put $user->getType()
because I know the object I have implements the UserInterface
but it all just seems wrong, even my IDE thinks so or am I missing something here?
Why does this not work? It is messing up my code if I have to put EntityInterface
instead of 'UserInterface
I think.
The problem lies here:
As the first step, inspect the definition of
AbstractMapper
:Interface definitions between parent and child classes are transitive, so we can merge the first definition:
This means that your class needs to implement:
And this is not possible, because method overloading doesn't exist in PHP.
Possible solution
Assuming the following interface definitions:
I would suggest to drop the
implements UserMapperInterface
: