1

Is this even possible? For example, say I have an array of Dogs. How do I get code completion to work? Here's code to illustrate the problem. Any advice would be great!

class Dog {

    private $name;

    public static function init_many(array $names) {
        foreach ($names as $n) {
            $collection[] = new self($n);
        }
        return $collection;
    }

    public function __construct($n) {
        $this->name = $n;
    }

    public function bark() {
        return sprintf('woof! my name is %s',
            $this->name
        );
    }
}

$Scoobi = new Dog('scoobi');
$Scoobi->                       // code hinting / completion works!

$dogs = Dog::init_many(array('scoobi', 'lassie', 'bingo'));
$dogs[0]->                      // code hinting / completion doesn't work!
jlee
  • 492
  • 5
  • 15
  • for those coming from Google, I found the answer here (top result for array code hinting): http://stackoverflow.com/questions/778564/phpdoc-type-hinting-for-array-of-objects – jlee Jan 07 '11 at 20:07

2 Answers2

1

In Zend Studio 11 I use :

/**
* 
* @return Dog[]
*/
public static function init_many(array $names) {
    foreach ($names as $n) {
        $collection[] = new self($n);
    }
    return $collection;
}
Virgili Garcia
  • 111
  • 1
  • 5
1

An indirect way to do this could be

$dogs = Dog::init_many(array('scoobi', 'lassie', 'bingo'));
foreach ($dogs as & $dog)
{
  /* @var $dog Dog */
  $dog->           //code hinting works here, 
                   //I use this all the time itereting over doctrine collections
}
Gabriel
  • 126
  • 1
  • 6