Features PhpStorm

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 🙂

Comments below can no longer be edited.

9 Responses to Customizable getter and setter method templates in PhpStorm

  1. Avatar

    rtuin says:

    May 4, 2011

    That’s a great new feature!

  2. Avatar

    OZ says:

    May 5, 2011

    Nice feature, thank you.
    Please, publish new EAP build 🙂

  3. Avatar

    Sean says:

    May 5, 2011

    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/


  4. Avatar

    Well Done says:

    May 5, 2011

    Well done!
    The same for Javascript would be great.

  5. Avatar

    lucifurious says:

    May 7, 2011

    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. Avatar

    Mark says:

    May 27, 2011

    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. Avatar

    Chance Garcia says:

    October 14, 2011

    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. Avatar

    Nogod says:

    March 25, 2012

    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?

Discover more