Jeff Puckett
Jeff Puckett

Reputation: 40861

How to document mixed array return type?

What is the correct syntax to document an array of mixed strings and ints?

public function toArray(): array
{
    return [
        'string',
        42,
    ];
}

Here are the options I've considered:

/**
 * @return string|int[]
 */

But this seems to indicate it would either be a string or an int[]

/**
 * @return string[]|int[]
 */

And this would seem to indicate either an array of strings or an array of ints.

Upvotes: 2

Views: 2083

Answers (2)

Er. Amit Joshi
Er. Amit Joshi

Reputation: 645

Have a look on this document

at the last last bottom of the page

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.

 @return (int|string)[]

Also have a look on return in case needed more details.

Upvotes: 3

Kerkouch
Kerkouch

Reputation: 1456

You can use

@return (int|string)[]

More details on phpdoc

Upvotes: 3

Related Questions