PHP + BBEdit + RegEx for code generation get/set functions

Here’s a handy way to generate getter/setter functions. I use BBEdit, but since this is based on Regular Expressions it could be done in just about any code editor.

Say you start a class and have defined the following private member variables:

private $ip_range_id;
private $subscriber_id;
private $low_ip;
private $high_ip;

The next thing I commonly would want to add to my class is a public function for each member variable that can be used for either getting or setting its value. So for $high_ip, I might want a functiont that looks something like this:

public function high_ip($high_ip=false)
{
    if($high_ip) {
        $this->high_ip = $high_ip;
        return $this->high_ip;
    } else {
        return $this->high_ip;
    }
}

This way if I have instantiated an object from this class called $ipr I can get the current value of the private $high_ip like this: $ipr->high_ip() and if I want to set the value of $ipr‘s $high_ip to be '127.0.0.1' I can do it like this: $ipr->high_ip('127.0.0.1').

Ok, but what about those RegExs and the code generation bit?

Since the getter/setter functions generally start off very similar, they are ripe for templates and templates are a good place to build generators. I’ve seen many approaches, and they have their benefits but this is so quick and easy I thought it might be helpful to someone.

1. Copy the private member declaration from the class into a temporary document (BBEdit’s “Scratchpad” is great for this, as is Emac’s *scratch* buffer).

2. Open the “Find…” dialog, and enter this RegEx in the “Find” field:

^private \$([\w]+);$

3. Paste this into the “Replace” field:


public function \1($\1=false)
{
    if($\1) {
        $this->\1 = $\1;
        return $this->\1;
    } else {
        return $this->\1;
    }
}

4. Make sure “Grep” and “Wrap around” are checked off, and then press the “Replace All” button, and voila!


public function ip_range_id($ip_range_id=false)
{
    if($ip_range_id) {
        $this->ip_range_id = $ip_range_id;
        return $this->ip_range_id;
    } else {
        return $this->ip_range_id;
    }
}
public function subscriber_id($subscriber_id=false)
{
    if($subscriber_id) {
        $this->subscriber_id = $subscriber_id;
        return $this->subscriber_id;
    } else {
        return $this->subscriber_id;
    }
}
public function low_ip($low_ip=false)
{
    if($low_ip) {
        $this->low_ip = $low_ip;
        return $this->low_ip;
    } else {
        return $this->low_ip;
    }
}
public function high_ip($high_ip=false)
{
    if($high_ip) {
        $this->high_ip = $high_ip;
        return $this->high_ip;
    } else {
        return $this->high_ip;
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s