Is it somehow possible to narrow return type in PHP7.1 type hints?
The following code causes fatal error Declaration of A::foo(): Obj must be compatible with IA::foo(): IObj, even through narrowing the return type does not break inheritance typing principles: Obj implements IObj, therefore parent class return type constraint will be always satisfied when Obj instance is returned.
interface IObj {}
class Obj implements IObj {}
interface IA {
function foo(): IObj;
}
class A implements IA {
function foo(): Obj {
return new Obj();
}
}
Am I doing something wrong, or is this a PHP drawback?