Pergunta

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!
Foi útil?

Solução

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
}

Outras dicas

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;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top