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.
How do I document the type "array of [type]" in PHPDoc?
From the documentation of phpDocumentor
The value represented by Type can be an array. The type MUST be defined following the format of one of the following options:
unspecified, no definition of the contents of the represented array is given. Example:
@return array
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.
Example:
@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.
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)[]
Note
many IDEs probably do not support this notation yet.