如何记录[type]数组?

时间:2021-10-31 16:21:49

Say I have a function like this:

说我有这样的功能:

function theFunction() {
    $arr = array();
    for ($i=0;$i<10;$i++) {
        $arr[] = new theObject($i);
    }
    return $arr;
}

I need to document the return type of the function. I could of course just usearray, but that does not provide all the information that can be provided, and doesn't tell the developer much about the true nature of the function.

我需要记录函数的返回类型。我当然可以使用array,但这并没有提供所有可以提供的信息,也没有告诉开发人员很多关于函数的真实性质。

How do I document the type "array of [type]" in PHPDoc?

如何在PHPDoc中记录“[type]”类型的类型?

3 个解决方案

#1


9  

From the documentation of phpDocumentor

从phpDocumentor的文档

The value represented by Type can be an array. The type MUST be defined following the format of one of the following options:

Type表示的值可以是数组。必须按照以下选项之一的格式定义类型:

  1. unspecified, no definition of the contents of the represented array is given. Example: @return array

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

  2. specified containing a single type, the Type definition informs the reader of the type of each array element. Only one Type is then expected as element for a given array.

    如果指定包含单个类型,则Type定义会通知读者每个数组元素的类型。然后,只需要一个Type作为给定数组的元素。

    Example: @return int[]

    示例:@return int []

    Please note that mixed is also a single type and with this keyword it is possible to indicate that each array element contains any possible type.

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

  3. specified containing multiple types, the Type definition informs the reader of the type of each array element. Each element can be of any of the given types. Example: @return (int|string)[]

    如果指定包含多个类型,则Type定义会通知读者每个数组元素的类型。每个元素可以是任何给定类型。示例:@return(int | string)[]

    Note
    many IDEs probably do not support this notation yet.

    请注意,许多IDE可能还不支持此表示法。

#2


1  

If I'm remembering right you supply the return type and a description, can you not put it in the description?

如果我记得你正确的提供了返回类型和说明,你能不能把它放在描述中?

/**
 * blah
 * @return array array of types
 */

#3


0  

In PHPDoc you can do the following for type hinting array members:

在PHPDoc中,您可以对类型提示数组成员执行以下操作:

@var array<\My\Folder\ClassName>

#1


9  

From the documentation of phpDocumentor

从phpDocumentor的文档

The value represented by Type can be an array. The type MUST be defined following the format of one of the following options:

Type表示的值可以是数组。必须按照以下选项之一的格式定义类型:

  1. unspecified, no definition of the contents of the represented array is given. Example: @return array

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

  2. specified containing a single type, the Type definition informs the reader of the type of each array element. Only one Type is then expected as element for a given array.

    如果指定包含单个类型,则Type定义会通知读者每个数组元素的类型。然后,只需要一个Type作为给定数组的元素。

    Example: @return int[]

    示例:@return int []

    Please note that mixed is also a single type and with this keyword it is possible to indicate that each array element contains any possible type.

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

  3. specified containing multiple types, the Type definition informs the reader of the type of each array element. Each element can be of any of the given types. Example: @return (int|string)[]

    如果指定包含多个类型,则Type定义会通知读者每个数组元素的类型。每个元素可以是任何给定类型。示例:@return(int | string)[]

    Note
    many IDEs probably do not support this notation yet.

    请注意,许多IDE可能还不支持此表示法。

#2


1  

If I'm remembering right you supply the return type and a description, can you not put it in the description?

如果我记得你正确的提供了返回类型和说明,你能不能把它放在描述中?

/**
 * blah
 * @return array array of types
 */

#3


0  

In PHPDoc you can do the following for type hinting array members:

在PHPDoc中,您可以对类型提示数组成员执行以下操作:

@var array<\My\Folder\ClassName>