SwiftedMind
SwiftedMind

Reputation: 4287

PHPStorm - type hinting with arrays

You can type hint arrays like this:

/** @var Class[] $variable */
$variable = /*someArrayWithObjects*/;

But is it possible to type hint the array as itself and the objects of the Array differently? Something like:

/** @var Class, SomeOtherClass[] $variable */
$variable = /*someArrayWithObjects*/;

So that the array itself shows methods from Class and every Object of the array shows methods from SomeOtherClass?

Upvotes: 3

Views: 995

Answers (1)

Arjan
Arjan

Reputation: 9874

Yes. You can specify that $variable is, for example, a Collection or an array of SomeOtherClass:

/** @var Collection|SomeOtherClass[] $variable */
$variable = /*someArrayWithObjects*/;

This will give you code completion for Collection on $variable and also for the methods in SomeOtherClass when you iterate over the items in $variable. This of course assumes that you can iterate over $variable.

Upvotes: 4

Related Questions