Trying to make an abstract class to partially implement functionality of its' child classes and force a contract upon them required for this implementation, I use the following construct:
abstract class Parent {
public static function fromDB(string $name = '') {
$instance = new static();
if (!empty($name)) {
$instance->setName($name)->read();
}
return $instance;
}
public abstract function read();
public abstract function setName(string $name): self;
}
Here PHP seems to understand that setName($name)
returns an Object of type Parent
, but PhpStorm indicates that read()
can not be called on the result, which would have been the expected result.
Error Message: Referenced Method is not found in subject class.
I do not understand why this happens, and even suspect a bug in PHP or PhpStorm.
I've read up on Late static binding and the following questions which partially talk about this problem, but I couldn't figure out how to fix it:
Thank you for your time and help.
I'm trying to implement setName in child classes in a way so it is clear that the type of the returned Object is the one of the child:
public function setName(string $name = null): user {...}
which doesn't work with self
return type and static
is forbidden.