Search code examples
phpnetbeansreturn-valuephpdoctype-hinting

Can you hint an array's items type?


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
  }

Solution

  • Use

    @return TClass3[]
    

    or

    @return TClass3[]|TClass3