3

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?

1 Answer 1

4

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.

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

Comments

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.