How to automatically generate PHP getter and setter using Eclipse? There are several possibilities, and no perfect one, but let’s try to list them all:
- Using Eclipse templates: semi-automatic
Window > Preferences > PHP > Editor > Templates -> New and use something like this:private $$${PropertyName}; ${cursor} public function get${PropertyName}() { return $$this->${PropertyName}; } public function set${PropertyName}($$value) { $$this->${PropertyName} = $$value; }
And then, to use this template type its name and press ctrl+space to generate this code. A context menu should also automatically appear when you type the name.
- Use the Eclipse PDT plugin E-Surf: automatic, useful but not very clean and their is no way to configure it.
- Use the Eclipse PDT PHPClassGenerator plugin by IBM to generate automatically classes and accessors: automatic but maybe to complex to configure and use. And it can’t generate getters / setters for attributes that have been added after the class creation.
If you don’t know yet how to install an Eclipse plugin: Let’s work OSGi Virgo on Eclipse (fr).
Humpf… At the moment, their is no perfect solution, and it is a shame! In Java, a simple right clic on a class > Source > Generate get/set or Generate toString… That is why developing in PHP on Eclipse is not yet as easy as developing in Java.