Code Completion for private/protected member variables when using magic __get()

走远了吗. 提交于 2019-11-28 19:19:05

问题


How do I setup code completion to work on Zend Studio (or any Eclipse based IDE) when working with a class that has private or protected member variables WITHOUT resorting to a bunch of Getter's OR setting the member vars as public.

For example:

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.

回答1:


Code Completion for Magic Methods can be achieved by using the @property and @method annotation in the DocBlock of the class (not in the Method Docs).

/**
 * @property string bark
 */
class Dog {
    /* ... */
}

$Dog = new Dog();
echo $Dog-> // will autocomplete now

Note that there is no correlation between the actual code and the annotation. Zend Studio will show whatever you set for @property, regardless of this property existing. It will also not check if there actually is a magic method available.



来源:https://stackoverflow.com/questions/3814733/code-completion-for-private-protected-member-variables-when-using-magic-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!