Improved Sublime Text 2 PHP getter and setter generation

As I've been using Sublime Text 2 for all coding for the last year, I've noticed a significant problem with my simple 'getset' snippet that I created last year: it doesn't handle underscores in property names correctly.

I've finally got around to fix it!

As with a lot of editors, Sublime Text supports snippets which are essentially text expansions of a short phrase into more text. This snippet creates a getXxx() and setXxx() method from a property name.

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

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

To use, simply type gs followed by tab and it will automatically expand and you can then type your underscore_separated property name and the correct method names will be created.

Alternatively, select some text and press shift+cmd+p and then type gs to automatically replace the selected text with the get and set methods completed for the text that was selected.

As an example, typing gs {tab} date_last_updated will produce:

I've also updated the snippet in my sublime-akrabat package on github.

3 thoughts on “Improved Sublime Text 2 PHP getter and setter generation

  1. This is an awesome snippet I really use a lot during development. The one modification I made is the option to type hint setters.

    The ($${1/_(w)/U$1/g$1}) becomes ($2$${1/_(w)/U$1/g$1}).

    First normal example:
    gs {tab} date_last_updated {tab}{tab}
    Shows above code

    However this:
    gs {tab} date_last_updated {tab} DateTime {tab}

    Will create this setter:

    public function setDateLastUpdated(DateTime $dateLastUpdated)
    {
    $this->date_last_updated = $dateLastUpdated;
    return $this;
    }

Comments are closed.