繁体   English   中英

PHPStorm 类型提示不同类型的数组

[英]PHPStorm Type hinting array of different types

是否可以在 PHPStorm 中键入提示具有不同对象类型的数组,即:

public function getThings()
{
    return array (new Thing(), new OtherThing(), new SomethingElse());
}

即使在构建数组之前单独声明它们似乎也不起作用。

您可以使用 phpdocs 以便 phpstorm 接受多种类型的数组,如下所示:

/**
* @return Thing[] | OtherThing[] | SomethingElse[]
*
*/
public function getThings()
{
    return array (new Thing(), new OtherThing(), new SomethingElse());
}

这种技术将使 phpstorm 认为该数组可以包含这些对象中的任何一个,因此它将为您提供所有三个的类型提示。 或者,您可以使所有对象扩展另一个对象或实现一个接口并键入提示,一旦对象或接口如下所示:

/**
* @return ExtensionClass[]
*
*/
public function getThings()
{
    return array (new Thing(), new OtherThing(), new SomethingElse());
}

这将为您提供有关类从父类或接口扩展或实现的内容的类型提示。

我希望这有帮助!

这在 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())
{
    <...>
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM