I have 3 classes like :
class Foo
{
static function test()
{
return new static();
}
}
class Bar extends Foo
{}
class Baz extends Foo
{}
Now if call :
$var = Bar::test();
I want PhpStorm to identify $var
as the called_class
, here: Bar
.
But, if I do $var = Baz::test();
$var
is Baz
instance.
How can I get the dynamic called_class to indicate to PhpStorm what type is returned?
I there a syntax like
/** @return "called_class" */
to help PhpStorm and indicate the type?
First you have an error in your static function. You can not use
return $this;
as the static call will not create any instance. So you have to create a new instance.
class Foo
{
public static function test()
{
return new static();
}
}
The static keyword will instantiate a new instance of the class itself.
class Bar extends Foo
{
public function fooBar(){}
}
class Baz extends Foo
{
public function fooBaz(){}
}
i just added the foo functions to show you that phpStorm now will correctly find the source.
$var = Bar::test();
$var->fooBar();
$var is now an instance of Bar
$var2 = Baz::test();
$var2->fooBaz();
$var2 is now an instance of Baz