Home » Php » php – Can you hint an array's items type?

php – Can you hint an array's items type?

Posted by: admin April 23, 2020 Leave a comment

Questions:

This question is linked to this one :

Is it possible to hint the type of the items inside a returned array ?

e.g. :

/**
 *  MyFunction does a lot of things
 *
 * @param TClass1 $var1
 * @param TClass2 $var2
 * @return array[TClass3] //<- I'm trying to express this
 */
 function MyFunction( $var1, $var2 ){
   ...

I am using NetBeans as an IDE, which takes (like many other PHP IDEs) advantage of the doc blocs above functions to determine the type of returned values.

If I could explain what type is expected inside an array, I could hope for the IDE to be able to offer correct completion for the following case :

  $myTab = MyFunction( $foo, $bar );
  foreach( $myTab as $itm ){
    $itm->myFi| //offer the completion for a TClass3 object
  }
How to&Answers:

Use

@return TClass3[]

or

@return TClass3[]|TClass3

Answer:

Are you trying to hint the return type so the documentation generates it? If so you could probably do it, you could also put it in the description of that variable.

It’s a comment, you’re allowed to do anything in it.