指定数组索引类型
Specify an array index type
如何指定数组索引和子索引类型? 注意:我会和PHPStorm一起使用。
数组示例:
function name ($options) {
// $options['length'] => integer
// $options['more'] => array
// $options['more']['test1'] => boolean
// $options['more']['test2'] => boolean
}
示例(无效):
/**
* @param array $options
* @var int $length
* @var array $more
* @var bool $test1
* @var bool $test2
*/
看起来,according to the docs,只能将数组定义为一个特定类型的集合(而不是为每个索引设置一个类型):
/**
* @param string[] $options
*/
更好的解决方案可能是将 $options
设为 class,因此 length
和 test1
可以是具有默认值和预定义类型的属性。
一般来说,PhpStorm 只支持简单的语法,正如 Sam 所说,例如
/**
* @param string[] $options
*/
上面的代码描述的参数是字符串数组。
安装选项完成插件 -- 它支持 PHPDoc 中新提议的散列语法(描述数组键及其类型):https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#7-describing-hashes
此插件将为数组键添加代码完成。
<?php
class Element {
/**
* 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())
{
// some code here
}
}
new Element(['label' => 'Bob', '|' ]);
// | Ctrl+Space will show supported attributes
注意:这个插件的主要目的是提供数组键补全——我不确定它对每个数组元素的类型解析支持有多好(如果它们与您的示例不同)。
如何指定数组索引和子索引类型? 注意:我会和PHPStorm一起使用。
数组示例:
function name ($options) {
// $options['length'] => integer
// $options['more'] => array
// $options['more']['test1'] => boolean
// $options['more']['test2'] => boolean
}
示例(无效):
/**
* @param array $options
* @var int $length
* @var array $more
* @var bool $test1
* @var bool $test2
*/
看起来,according to the docs,只能将数组定义为一个特定类型的集合(而不是为每个索引设置一个类型):
/**
* @param string[] $options
*/
更好的解决方案可能是将 $options
设为 class,因此 length
和 test1
可以是具有默认值和预定义类型的属性。
一般来说,PhpStorm 只支持简单的语法,正如 Sam 所说,例如
/**
* @param string[] $options
*/
上面的代码描述的参数是字符串数组。
安装选项完成插件 -- 它支持 PHPDoc 中新提议的散列语法(描述数组键及其类型):https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#7-describing-hashes
此插件将为数组键添加代码完成。
<?php
class Element {
/**
* 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())
{
// some code here
}
}
new Element(['label' => 'Bob', '|' ]);
// | Ctrl+Space will show supported attributes
注意:这个插件的主要目的是提供数组键补全——我不确定它对每个数组元素的类型解析支持有多好(如果它们与您的示例不同)。