Assume I have three classes like so.
abstract class A {
abstract protected function doSomething($object): array;
}
class B extends A {
protected function doSomething(SomeObject $object): array
{
// something
}
}
class C extends A {
protected function doSomething(OtherObject $object): array
{
// something
}
}
According to principles of inheritance as PHP does it, the structure described above is not legal PHP code, as the method definitions are not compatible with the base class. I can, of course, do something like
class B extends A {
protected function doSomething($object): array
{
if (!is_a($object, 'SomeObject')) {
throw new Exception('Object passed to method is not of the correct class for this method.');
}
// something
}
}
But I was wondering if there is another solution to implementing a method in an abstract class that can be redefined to only accept objects of specific type in it.