Artem Russakovskii Artem Russakovskii - 1 year ago 168
PHP Question

PHPDoc type hinting for array of objects?

So, in PHPDoc one can specify

above the member variable declaration to hint at its type. Then an IDE, for ex. PHPEd, will know what type of object it's working with and will be able to provide a code insight for that variable.

class Test
/** @var SomeObj */
private $someObjInstance;

This works great until I need to do the same to an array of objects to be able to get a proper hint when I iterate through those objects later on.

So, is there a way to declare a PHPDoc tag to specify that the member variable is an array of
array is not enough, and
@var array(SomeObj)
doesn't seem to be valid, for example.

Answer Source

The best you can do is say,

foreach ($Objs as $Obj)
    /* @var $Obj Test */
    // You should be able to get hinting after the preceding line if you type $Obj->

I do that a lot in Zend Studio. Don't know about other editors, but it ought to work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download