Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不同类型的PHPStorm类型提示数组_Php_Phpstorm_Type Hinting - Fatal编程技术网

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

不同类型的PHPStorm类型提示数组,php,phpstorm,type-hinting,Php,Phpstorm,Type Hinting,在PHPStorm中是否可以键入具有不同对象类型的数组,即: public function getThings() { return array (new Thing(), new OtherThing(), new SomethingElse()); } 即使在构建数组之前单独声明它们似乎也不起作用。您可以使用phpdocs,以便phpstorm接受多种类型的数组,如下所示: /** * @return Thing[] | OtherThing[] | SomethingElse[

在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标准中有描述

/**
*使用给定的选项初始化该类。
*
*@param数组$options{
*@var bool$required是否需要此元素
*@var string$标记此元素的显示名称
* }
*/
公共函数构造(数组$options=array())
{
}

是。“PHP5引入了类型暗示。函数现在可以强制参数成为对象(通过在函数原型中指定类的名称)、接口、数组(从PHP5.1开始)”——除非我完全误解了你的问题。我说的是PHPStorm(IDE)@ᵈ啊,好的,解决了我对这个问题的困惑。My bad.PhpStorm不支持单个数组元素的类型暗示(尤其是使用数字键)。现在存在的这些票据都是关于传入数据的(描述数组类型的参数结构)。可能的建议:
list($a,$b,$c)=$object->getThings()然后键入提示单个变量。如果此对象实现一个接口,则为bossile。将phpdoc注释添加到方法中。Phpstorm在您迭代此对象数组时建议对象方法。您可以编写
@return(Thing | OtherThing | SomethingElse)[
。不是说有3种可能的返回类型,而是说唯一的返回类型是数组,但数组值可以是这些不同的类型。这是一个微妙的区别。目前(2019年9月),该链接没有帮助,因为该PSR中的标签列表已被删除,尚未转换为新的PSR。但是,这在将来的某个时候可能会发生。这是一个很好的答案,但不是这里所问的答案;)
/**
 * 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())
{
    <...>
}