PHP Method Chaining

A post that came from my personal blog that now just points to the hawk host blog.  I figured this was worth transporting so here it is.  I have no idea how accurate it is or if the code 100% works but I believe it does.  If you do find any mistakes just post about them in the comments and I’ll correct them.

I’ve been reading up on method chaining in PHP recently.  You’d be surprised how useful it can actually be in some cases, so I figured I’d share. First of all method chaining is not for all circumstances by any means but it can make doing some code a lot easier and it can also be easier to understand.

First of all what is method chaining when doing it with PHP? The easiest example I can think of is constructing a database query.


$db = new Database;
$db->select("name,email")->from("customers")->where("name='joe'")->limit("0,5")->execute();

In this case it doesn’t make a whole lot of sense to be using it. You’re constructing a database query with a bunch of functions now but it’s the easiest to understand when you’re first learning about it. So now lets look at a better example here of when it is actually really useful. In this case I’m going to create a simple validation system using method chaining.


$validation = new Validation();
$myvar = $_POST['somepostvariable'];
if ($validation->value($myvar)->required()->isInt()->execute())
{
print 'validated';
}
else
{
print 'not validated';
}

A very simple example there of it’s power I tried to lay it out as simple as possible so extra declarations and such. Now here’s what it would look like if we were to build the same function not using chaining.


$validation = new Validation();
$myvar = $_POST['somepostvariable'];
if ($validation->required($myvar) && $validation->isInt($myvar))
{
print 'validated';
}
else
{
print 'not validated';
}

So how did I create this chain? Well I first need PHP5 and here’s the actual code of the Validation class


Class Validation
{
public $value = '';
public $error_count = 0;

public function __construct()
{

}

public function value($value)
{
$this->value = $value;
return $this;
}

public function required()
{
if (empty($this->value))
{
$this->error_count++;
}
return $this;
}

public function isInt()
{
if (!is_numeric($this->value) || intval($this->value) !=$this->value)
{
$this->error_count++;
}
return $this;
}

public function execute()
{

if ($this->error_count>0)
{
$this->error_count = 0;
return false;
}
else
{
return true;
}
}

}

So now how does this work? Well in PHP5 you can return objects so using this ability we return the object on each method that we want to be part of the chain. How we handle variables in this instance is make use of class based variables using $this->var. So we just increase the error count and finally use a final function which tells us that our chain has ended which will do the checks for us. Of course this is a very basic example and I do not actually use this code, but I hope someone will find it useful when looking for a good method chaining example.

This entry was posted in General. Bookmark the permalink.

4 Responses to PHP Method Chaining

  1. ss says:

    Nice code. Nice example.

  2. Caleb says:

    Nice, but I would have loved to see the database class

  3. CoursesWeb says:

    Hi,
    To return the class instance in the static method (to chain it), you can use:
    return new self; .

  4. Bhumi says:

    Very Nice example with detailed explanation about Method Chaining in php. Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *