Eds*_*ina 7 php type-hinting phpstorm
是否可以在PHPStorm中使用不同的对象类型键入提示数组,即:
public function getThings()
{
return array (new Thing(), new OtherThing(), new SomethingElse());
}
Run Code Online (Sandbox Code Playgroud)
即使在构建数组之前分别声明它们似乎也不起作用。
小智 7
您可以使用phpdocs来使phpstorm接受多个类型的数组,如下所示:
/**
* @return Thing[] | OtherThing[] | SomethingElse[]
*
*/
public function getThings()
{
return array (new Thing(), new OtherThing(), new SomethingElse());
}
Run Code Online (Sandbox Code Playgroud)
这种技术将使phpstorm认为该数组可以包含这些对象中的任何一个,因此它将为您提供所有这三个对象的类型提示。或者,您可以使所有对象扩展另一个对象或实现一个接口,并键入提示,一旦对象或接口如下所示:
/**
* @return ExtensionClass[]
*
*/
public function getThings()
{
return array (new Thing(), new OtherThing(), new SomethingElse());
}
Run Code Online (Sandbox Code Playgroud)
这只会为您提供有关类从父类或接口扩展或实现的类型的提示。
希望对您有所帮助!
这在 PHPDoc 标准中有描述
https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#713-param
/**
* Initializes this class with the given options.
*
* @param array $options {
* @var bool $required Whether this element is required
* @var string $label The display name for this element
* }
*/
public function __construct(array $options = array())
{
<...>
}
Run Code Online (Sandbox Code Playgroud)