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.

5
  • 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)" php.net/manual/en/language.oop5.typehinting.php - Unless I totally misread your question. Commented Mar 20, 2015 at 13:08
  • 1
    I'm talking about PHPStorm (an IDE) @ʰᵈˑ Commented Mar 20, 2015 at 14:09
  • Ah ok, that edit resolved my confusion with the question. My bad. Commented Mar 20, 2015 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. Commented Mar 20, 2015 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. Commented Mar 21, 2015 at 19:07

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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())
{
    <...>
}

2 Comments

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.
This is a good answer to a different question, but not the one asked here ;)
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.

1 Comment

It doesn't work from what I can see. This notation defines a single type of collection or key type and item type. More than two arguments do nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.