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

This blog is permanently closed.

For up-to-date information please follow to corresponding WebStorm blog or PhpStorm blog.

 
This entry was posted in Cool Feature, PhpStorm and tagged , . Bookmark the permalink.

9 Responses to Customizable getter and setter method templates in PhpStorm

  1. rtuin says:

    That’s a great new feature!
    Thanks

  2. OZ says:

    Nice feature, thank you.
    Please, publish new EAP build :)

  3. Sean says:

    RE: OZ Says:
    Nice feature, thank you.
    Please, publish new EAP build

    They did publish this functionality in the latest EAP Release: http://blogs.jetbrains.com/webide/2011/04/phpstorm-webstorm-2-1-eap-build-107-21/

    Cheers!

  4. Well Done says:

    Well done!
    The same for Javascript would be great.

  5. lucifurious says:

    This is awesome, but you need to add one more tiny little tweak! ;)

    If a setter returns “$this” it should automatically insert the @return {returnType} (if detected of course). As it stands, one must do it in two or more steps. Quite counterproductive, especially since I tend to have all my setters return $this for chaining.

  6. Mark says:

    This is a fantastic feature! It would be great though if we could add “@return ${CLASS_NAME}” to the PHPDoc so it validates correctly.

    Vote it up! http://youtrack.jetbrains.net/issue/WI-6472

    Cheers and thanks!

  7. I’d really love to see a way for the getters and setters to be placed together at near the top. Just an organizational thing I have, probably from not using an IDE before and therefor no declaration detection.

  8. Nogod says:

    Guys, i don’t fully understand. In your example you put phpdoc comment to setter, but it wasn’t generated. I have same issue, is it a feature?

Comments are closed.