Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.
like image 722
jlee Avatar asked Sep 28 '10 16:09

jlee


People also ask

What is __ get?

__get() is utilized for reading data from inaccessible properties.

Why we use magic methods in PHP?

Magic methods are special methods which override PHP's default's action when certain actions are performed on an object. All methods names starting with __ are reserved by PHP. Therefore, it is not recommended to use such method names unless overriding PHP's behavior.

Which method called when your object attempt to read property or variable of the class which is inaccessible or unavailable?

__set : __set is called when object of our class attempts to set value of the property which is really inaccessible or unavailable in our class.


1 Answers

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

like image 184
Gordon Avatar answered Sep 18 '22 19:09

Gordon