views:

38

answers:

1

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  A: 

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.

Code Completion in Zend Studio with @property annotation

Gordon
Zend Studio will see the protected specifier and still hide it from auto-complete, no?
webbiedave
@webbiedave If you annotate as shown above and you type `$dog->` the code completion will pop up and include `bark` in the available properties list. Zend Studio will show whatever is written for `@property`. There does not have to exist a property of that name internally. It will just show what you put in the annotation. If you put `meow` up there, it will show that too, despite the property not existing. You dont even have to have a `__get` method. There is no correlation between the actual code, the completion and the annotations.
Gordon
Cool, Gordon. Thanks.
webbiedave