正确的方法来表示phpDocumentor的数组数据类型?

时间:2012-08-09 16:31:40

标签: php documentation phpdoc

以下哪项是为phpDocumentor记录此方法的返回类型的正确方法?

方法1:

/**
 * @return array Foo array.
 */
public function foo() {
    return array(1, 2, 3);
}

方法2:

/**
 * @return integer[] Foo array.
 */
public function foo() {
    return array(1, 2, 3);
}

此外,这两种方法都有任何IDE含义吗?

修改

似乎PhpStorm和Netbeans 7.1+ IDE都支持第二种方法。

2 个答案:

答案 0 :(得分:14)

这两种方法在技术上都是正确的,但这种方法被认为是“更好”,因为它更具体(intinteger可以互换):

@return int[]

此处记录:

http://www.phpdoc.org/docs/latest/guides/types.html

答案 1 :(得分:5)

在撰写此答案时,这些是phpDocumentor(可能还有其他PHPDoc实现)表示数组的可接受方式:

  
      
  1. 未指定,没有所代表数组内容的定义   给出。示例:@return array

  2.   
  3. 指定包含单一类型Type定义通知   每个数组元素类型的读者。那么只有一个Type   期望作为给定数组的元素。   示例:@return int[]

      请注意,mixed也是一种类型,并使用此关键字   可以指示每个数组元素包含任何可能的   类型。

  4.   
  5. 指定包含多种类型Type定义通知   每个数组元素类型的读者。每个元素都可以是任何元素   给定类型。示例:@return (int|string)[]
  6.