Customizable getter and setter method templates in PhpStorm
Another EAP build of PhpStorm 2.1 comes with bundled templates for PHP getter and setter methods. You can change these templates for your own needs in Settings|File Templates|Code|PHP Getter/Setter Method.
For example, here is a default template for setter method:
Just like other PhpStorm templates it uses Apache Velocity template language syntax.
Let’s assume that we have the following code:
By invoking “Add getter and setter” quick fix on $_bar
field, you will get the following setter method:
But now I want setBar()
to return $this
. I’m adding an extra line "return $this"
to setter method template:
If I delete and generate the setter method again it will look as follows:
I can go even further and use an exact field name with underscore in my setter method name. I change ${NAME}
to ${FIELD_NAME}
as shown here:
If I regenerate the setter again, it will be:
Note that “Unused private field” inspection will check this updated method name too. In other words it will find the correct setter method “set_bar” and will not report the field $_bar
as unused.
If you suddenly realize that you have screwed up your template and would like to return to its default version, you can always do this by pressing “Reset to Default” button in File Templates toolbar:
Perhaps that’s the maximum customization flexibility you can get for automatically generated methods. Use it wisely :-)