phpDocumentor支持声明一个只包含特定类型项目的数组,例如: http://www.phpdoc.org/docs/latest/guides/types.html#arrays中所述的SomeObject[]
或string[]
。但是,我不确定这样的符号是否仅表示指定类型的序列(列表),或者是否适合将它用于词典(哈希)。
这种用法无疑是正确的:
/** @var SomeObject[] $myList */
foreach($myList as $item) {
// … Do something with the $item.
// Every $item is an instance of SomeObject[]
}
但这种用法也可以吗?
/** @var SomeObject[] $myDictionary **/
foreach($myDictionary as $ref => $item) {
// … Do something not only with the $item, but with
// the non-sequential $ref too.
// Although every $item is an instance of SomeObject,
// there is no clue, what the keys ($ref) are.
}
或者说,没有foreach
:
/**
* @param SomeObject[] $dictionary
* @param Ref $refObject
* @return SomeObject
*/
function getByRef(array $dictionary, Ref $refObject) {
// Just added a bit of complexity so that the example
// is not totally dumb.
$key = $refObject->getDictionaryKey();
return $dictionary[$key];
}
答案 0 :(得分:0)
您无法记录密钥,但您可以指定密钥类型。
@var array[string]string