Sublime Text 2 Snippet for PHP getter and setter generation

Update: This article has been superseded by Improved Sublime Text 2 PHP getter and setter generation

I've been playing with Sublime Text 2 recently and have quite enjoyed how quiet my ageing laptop is when the fans aren't running due to a Java-based IDE.

As with a lot of editors, Sublime Text supports snippets which are essentially text expansions of a short phrase into more text. I needed to create a few getXxx() and setXxx() methods for some properties of a class and decided that the easiest way to do this would be with a snippet.

To create a snippet, go to Tools->New Snippet... and replace the code example provided with this:

<snippet>
    <content><![CDATA[public function get${1/(.*)/\\u$1/}()
{
    return \\$this->${1:$SELECTION};
}

public function set${1/(.*)/\\u$1/}(\\$$1)
{
    \\$this->$1 = \\$$1;
    return \\$this;
}
]]></content>
    <!-- Optional: Tab trigger to activate the snippet -->
    <tabTrigger>getset</tabTrigger>
    <!-- Optional: Scope the tab trigger will be active in -->
    <scope>source.php</scope>
    <!-- Optional: Description to show in the menu -->
    <description>Create getter and setter methods</description>
</snippet>

Save the file as getset.sublime-snippet and you're done.

To use, simply type getset followed by tab (in the latest dev builds, at least) and it will automatically expand. Alternatively, select some text and use shift+cmd+p -> getset to automatically replace the selected text with the get and set methods completed for the text that was selected.

9 Responses to “Sublime Text 2 Snippet for PHP getter and setter generation”

  1. 1 Mathieu

    Hello,

    I have "Error parsing content for snipper"

    Do you know why ?

  2. 2 Markus Fischbacher

    I like Sublime Text 2 but mostly using Netbeans instead. I realy like the clean UI but its missing some features i realy dont like to miss.

  3. 3 Markus Fischbacher

    hmmm... after diving a bit more into sublime 2 i could reproduce some of the missing features with packages and snippets.

    what i realy missing is a "goto reference" or "goto class" feature.

  4. 4 Rob...

    Marcus,

    Look at SublimeCodeIntel and the SublimeText/CTags packages.

    Package control is worth checking out too.

  5. 5 Markus Fischbacher

    Great thank you. I will have a look at this two.
    Any other Packages you could advise?

  6. 6 Markus Fischbacher

    lol ... CodeIntel is great but CTags drived me crazy! After installing CTags i wasn't able to use \ key (german layout) anymore. took about an hour to find this out.

  7. 7 sergiu

    I have a problem. I saved file how it si shown there, but it doesn't work...

    What can be the problem??

  8. 8 Rob...

    Sergiu, look for PHP Snippets in Package Control.

  9. 9 Eric Burns

    This is really cool. I have a particular use case where I'm using Doctrine 2 and it helps for all my member variables to be named with underscores like foo_bar but I want camel case getters and setters like getFooBar() and setFooBar(). Tweaked your snippet a bit and it seems to work great:

    ${1/([a-z]+)([A-Z][a-z]+)/\1_\l\2/g$1};
    }

    public function set${1/(.*)/\u$1/}(\$$1)
    {
    \$this->${1/([a-z]+)([A-Z][a-z]+)/\1_\l\2/g$1} = \$${1:$SELECTION};
    return \$this;
    }
    ]]>

    getset

    source.php

    Create getter and setter methods

    Thanks for giving me most of what I needed :-)

The views expressed in these comments are not the views of the publisher. However, we believe in the rights of others to express their legitimate views and concerns. Any legitimate complaint emailed to rob@akrabat.com will be seriously considered and the post reviewed as desirable and necessary.