رمز التلميح / إكمال مجموعة من الكائنات في Zend Studio (أو أي IDE الأخرى القائمة على Eclipse)

StackOverflow https://stackoverflow.com/questions/4329288

سؤال

هل هذا ممكن؟ على سبيل المثال ، قل أن لدي مجموعة من الكلاب. كيف يمكنني إكمال رمز العمل؟ فيما يلي رمز لتوضيح المشكلة. أي نصيحه تكون ممتازة!

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!
هل كانت مفيدة؟

المحلول

يمكن أن تكون طريقة غير مباشرة للقيام بذلك

$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
}

نصائح أخرى

في Zend Studio 11 أستخدمه:

/**
* 
* @return Dog[]
*/
public static function init_many(array $names) {
    foreach ($names as $n) {
        $collection[] = new self($n);
    }
    return $collection;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top