PHPDoc SomeObject []表示法仅用于序列吗?

时间:2016-01-27 06:17:23

标签: php arrays dictionary hashmap phpdoc

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];
}

1 个答案:

答案 0 :(得分:0)

您无法记录密钥,但您可以指定密钥类型。

@var array[string]string