问题描述
我或多或少有这样的代码:
I have code more or less like this:
class Foo {
public static function factory($str) {
$class = "Foo_" . $str;
return new $class;
}
}
class Foo_Bar {
public function razzle() {
print "Foo_Bar->baz() was called";
}
}
$Obj = Foo::factory('Bar');
,我希望PhpStorm理解$Obj
是Foo_Bar
对象,因此,例如,如果我键入$Obj->raz
,则会显示razzle()
以进行自动补全.
and I would like PhpStorm to understand that $Obj
is a Foo_Bar
object, so that for example if I type $Obj->raz
, razzle()
will show up for autocompletion.
有什么办法可以做到这一点?告诉PhpStorm函数Foo::factory($str)
返回类型为Foo_$str
的对象?我的猜测是答案是否定的.
Is there any way to get this? To tell PhpStorm that the function Foo::factory($str)
returns an object of type Foo_$str
? My guess is that the answer is no.
推荐答案
是的,如果您的工厂是静态方法,您可以执行此操作.对于您提供的示例,它应该可以正常工作.
Yes, you can do this if your factory is a static method. It should work fine for the examples you have provided.
更多详细信息和一些基本示例可以在此处找到: http://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata
More details and some basic example can be found here: http://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata
这篇关于PhpStorm类型提示为工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!