I will be using the following example to illustrate my question:
class Attribute {}
class SimpleAttribute extends Attribute {}
abstract class AbstractFactory {
abstract public function update(Attribute $attr, $data);
}
class SimpleFactory extends AbstractFactory {
public function update(SimpleAttribute $attr, $data);
}
If you try to run this, PHP will throw a fatal error, saying that the Declaration of SimpleFactory::update() must be compatible with that of AbstractFactory::update()
I understand exactly what this means: That SimpleFactory::update()
s method signature must exactly match that of its parent abstract class.
However, my question: Is there any way to allow the concrete method (in this case, SimpleFactory::update()
) to redefine the type hint to a valid descendant of the original hint?
An example would be the instanceof
operator, which would return true in the following case:
SimpleAttribute instanceof Attribute // => true
I do realize that as a work around, I could make the type hint the same in the concrete method, and do an instanceof check in the method body itself, but is there a way to simply enforce this at the signature level?
abstract public function update(Attribute $attr, $data)
andabstract public function update(SimpleAttribute $attr, $data)
could co-exist in the same class scope. – Cann