12

Is it possible in PHPStorm to type hint an array with different object types, ie:

public function getThings()
{
    return array (new Thing(), new OtherThing(), new SomethingElse());
}

Even declaring them separately before building the array doesn't seem to work.

Edson Medina
  • 9,862
  • 3
  • 40
  • 51
  • Yes. "PHP 5 introduces type hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype), interfaces, **arrays (since PHP 5.1)**" http://php.net/manual/en/language.oop5.typehinting.php - Unless I totally misread your question. – ʰᵈˑ Mar 20 '15 at 13:08
  • 1
    I'm talking about PHPStorm (an IDE) @ʰᵈˑ – Edson Medina Mar 20 '15 at 14:09
  • Ah ok, [that edit](http://stackoverflow.com/revisions/29165580/2) resolved my confusion with the question. My bad. – ʰᵈˑ Mar 20 '15 at 14:11
  • 3
    PhpStorm does not support type hinting of individual array elements (especially with numeric keys). Those tickets that exist right now are all about incoming data (describe structure of parameter of an array type). Possible suggestion: `list($a, $b, $c) = $object->getThings();` and then typehint individual variables. – LazyOne Mar 20 '15 at 14:26
  • If this objects implement one interface it is bossile. Add phpdoc comment to the method. Phpstorm suggest object methods when you iterate over array of this objects. – funivan Mar 21 '15 at 19:07

3 Answers3

10

you can use phpdocs in order for phpstorm to accept an array of multiple types like so:

/**
* @return Thing[] | OtherThing[] | SomethingElse[]
*
*/
public function getThings()
{
    return array (new Thing(), new OtherThing(), new SomethingElse());
}

This technique will make phpstorm think that the array could contain any of those objects and so it will give you type hinting for all three. Alternatively you can make all of the objects extend another object or implement an interface and type hint that once object or interface like so:

/**
* @return ExtensionClass[]
*
*/
public function getThings()
{
    return array (new Thing(), new OtherThing(), new SomethingElse());
}

This will give you type hints for only what the classes extend or implement from the parent class or interface.

I hope this helped!

Jason Gallavin
  • 403
  • 4
  • 14
  • 3
    You can write `@return (Thing|OtherThing|SomethingElse)[]`. Instead of saying there are 3 possible returns types, you say that the only return type is an array, but that the array values can be of these different types. It's a subtle difference. – beardedlinuxgeek May 17 '16 at 17:22
5

This is described in the PHPDoc standards

https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#713-param

/**
 * Initializes this class with the given options.
 *
 * @param array $options {
 *     @var bool   $required Whether this element is required
 *     @var string $label    The display name for this element
 * }
 */
public function __construct(array $options = array())
{
    <...>
}
beardedlinuxgeek
  • 1,652
  • 16
  • 26
  • Currently (Sept 2019) that link is not helpful because the list of tags has been removed in that PSR and hasn't been converted to a new PSR yet. However, that is expected to happen at some point in the future. – AndreKR Sep 08 '19 at 09:24
  • This is a good answer to [a different question](https://stackoverflow.com/questions/15414103/best-way-to-document-array-options-in-phpdoc), but not the one asked here ;) – Jake Sep 13 '19 at 23:49
0

In PHP, I've seen a very nice way of doing this:

@return array<Thing,OtherThing,SomethingElse>

IDEs like PHPStorm and VSCode understand this syntax pretty well. Hope this helps.

Derick Alangi
  • 1,080
  • 1
  • 11
  • 31