Aug 29
  • english
  • german

If you program PHP code with Eclipse PDT, you surely missed the auto generate getter and setter refactoring option which is known from the JDT. So you had to write hundreds of thousands of lines of code to create those methods for the uncountable amount of data objects you wrote until now.

To speed things up, here's a solution with templates:

Click Window - Preferences - PHP - Templates and create a new template with the following content:

/**
*    Set the ${word_selection} value
*    @param unknown_type $$${word_selection}
*/
public function set${variable}($$${word_selection}) {
$$this->${word_selection} = $$${word_selection};
}

/**
*    Returns the ${word_selection} value.
*    @return unknown_type
*/
public function get${variable}() {
return $$this->${word_selection};
}
${cursor}

To use this template, type the template shortcut you have chosen in a PHP file of your choice (e.g. "getset"), followed by a variable name of your class (z.B. getsetcolor). Now highlight the word "color" (or yours, if different) and then press ctrl+space. Voila, there you have the desired methods. The cursor is on the right position for the variable type in the javadoc comment. Type "string" (or your type) and then jump to the method names with tab. You should type e.g. "Color" (starts with capital letter) and that's it!

This method should save you at least 1-2 minutes per property - try it!

7 Antworten zu “Generate getter and setter with Eclipse PDT”

  1. pandafahrer sagt:

    Leider funktioniert das mit der neuen PDT 2.0 nicht mehr, wenn man den Variablennamen hinter “getset” markiert, steht die Autovervollstöndigung für das Code-Template nicht mehr zur Verfügung.

    Hat schon jemand eine Lösung für dieses Problem gefunden?

  2. Sebastian sagt:

    Dafür wüsste ich auch gerne eine Lösung. In Ganymed gibt es mit der Codecompletetion eh ein größeres Problem.

  3. pandafahrer sagt:

    Ich hab zwischenzeitlich Netbeans for PHP ausprobiert, da funktioniert einiges besser.

    Denke, ich werde erstmal dabei bleiben.

  4. D.N. sagt:

    Hallo zusammen,
    ich habe eine Lösung dafür gefunden:
    1. Template siehe unten erstellen, mit dem Namen “getset”
    2. “get” schreiben und “Strg” + “Space” drücken, getset auswählen
    3. jetzt den Namen der Variable schreiben, er wird auto-replaced an allen relevanten Stellen.

    CODE:
    ——————————–
    /**
    * Set the ${bare_field_name} value
    * @param ${type} $$${bare_field_name}
    */
    public function set${bare_field_name}( $$param_${bare_field_name} ) {
    $$this->${bare_field_name} = $$param_${bare_field_name};
    }

    /**
    * Returns the ${bare_field_name} value.
    * @return ${type}
    */
    public function get${bare_field_name}() {
    return $$this->${bare_field_name};
    }
    ——————————–

  5. M.R. sagt:

    Vielen Dank an D.N. klappt wunderbar. Habe mein Template ein bisschen angepasst für meine Bedürfnisse:

    CODE:
    ——————–

    /**
    * Sets the ${field} value
    *
    * @param ${type} ${dollar}${field}
    * @return void
    */
    public function set${field_camelcase}(${dollar}${field}) {
    ${dollar}this->${field} = ${dollar}${field};
    }

    /**
    * Returns the ${field} value
    *
    * @return ${type}
    */
    public function get${field_camelcase}() {
    return ${dollar}this->${field};
    }

    ——————–

  6. verena sagt:

    @M.R.: die Variable ${field_camelcase} gibt es bei mir (Eclipse Galileo /PDT 2.1) nicht. Gibt es die nicht mehr, oder kommt die aus irgend einem anderen Plugin?

    Danke @all für dieses Template.

  7. Viru sagt:

    @verena:

    Man kann in den Templates seine eigenen Variablen verwenden ich vermute mal ${field_camelcase} hat M.R. wohl genommen um nen kleinen Erinnerer zu haben, dass der Name der Variablen hier in CamelCase schreibweise einzutragen ist

Einen Kommentar schreiben