PHP Method Chaining

Posted By: Tony Baird

Last Updated: Thursday June 4, 2009

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.

[sourcecode language=‘php’]

$db = new Database; $db->select(“name,email”)->from(“customers”)->where(“name=‘joe’”)->limit(“0,5”)->execute();

[/sourcecode]

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.

[sourcecode language=‘php’]

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

[/sourcecode]

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.

[sourcecode language=‘php’]

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

[/sourcecode]

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

[sourcecode language=‘php’]

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; } }

}

[/sourcecode]

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.

Ready to get started? Build your site from
$2.24/mo
GET STARTED NOW