在使用具有私有或受保护成员变量的类时,如何在Zend Studio(或任何基于
Eclipse的IDE)上设置代码完成功能,而无需使用一组Getter的OR将成员vars设置为public.
例如:
class Dog {
protected $bark = 'woof!';
public function __get($key) {
if (isset($this->$key)) {
return $this->$key;
}
}
}
$Dog = new Dog();
echo $Dog->bark; // <-- I want the IDE to "kNow" that bark is a property of Dog.
魔法的代码完成方法可以通过在类的DocBlock中使用
@property和
@method注释来实现(而不是在方法文档中).
/**
* @property string bark
*/
class Dog {
/* ... */
}
$Dog = new Dog();
echo $Dog-> // will autocomplete Now
请注意,实际代码和注释之间没有相关性. Zend Studio将显示您为@property设置的任何内容,而不管此属性是否存在.它也不会检查是否有一个魔术方法可用.