Prajwal Tuladhar’s Blog
 
programming, life and some random thoughts

Dec 27 2008

Method Chaining in PHP

Published by at 12:19 am under PHP

Make modifier methods return the host object so that multiple modifiers can be invoked in a single expression. – Martin Fowler [article]

In simple terms method chaining is the way of calling a sequence of class methods where each object returns the same host object for further modification. This approach is more used by JAVA and .NET developers (as I have seen). After having a quick look at various PHP frameworks like Zend, CodeIgniter and Cake, it seems like they do support method chaining and I really love to use method chaining when possible. Enabling method chaining is quite similar in PHP as it is in C# and JAVA. By the use of $this, method chaining can be enabled for any class.

<?php
	class HardDrive
	{
		private $_capacity;
		private $_isExternal;
		private $_speed;

		//Getters and setters for _capacity
		function getCapacity()	{
			return $this->_capacity;
		}

		function setCapacity($capacity)	{
			$this->_capacity = $capacity;
			return $this;
		}

		//Getters and Setters  for _isExternal
		function getIsExternal()	{
			return $this->_isExternal;
		}

		function setIsExternal($isExternal)	{
			$this->_isExternal = $isExternal;
			return $this;
		}

		//Gettter and setter for speed
		function getSpeed()	{
			return $this->_speed;
		}

		function setSpeed($speed)	{
			$this->_speed = $speed;
			return $this;
		}
	}
?>

Using Method Chaining for above Class

<?php
	$hardDrive = new HardDrive();
	$hardDrive
		->setCapacity(200)
		->setIsExternal(false)
		->setSpeed(7200);
?>

 

…or using traditional way

<?php
	$hardDrive = new HardDrive();
	$hardDrive->setCapacity(200);
	$hardDrive->setIsExternal(false);
	$hardDrive->setSpeed(7200);
?>

I have seen people who really hates method chaining and also seen people that use this approach a lot. And luckily or unluckily I am also kinda supporter of method chaining. So, if you are like me I hope you want to enable method chaining in your class as well and as you saw it’s very very simple.

And finally, some considerations you need to know. I don’t think method chaining is possible in PHP 4 (but who cares about PHP 4, coz it’s kinda RIP :) ). The biggest problem with PHP method chaining I have felt is that IDE like Eclipse really don’t provide auto-complete feature :(


4 responses so far

4 Responses to “Method Chaining in PHP”

  1. Anishon 28 Dec 2008 at 10:13 pm

    Besides code reduction and improved readability why do you think we should enable Method Chaining?

  2. Prajwal Tuladharon 28 Dec 2008 at 10:58 pm

    Code reduction and improved readability are the primary advantages of method chaining. Beside those, I like method chaining because I came from C# background so, it's kinda habit or call it influence.
    Most programmers esp. from JAVA and C# may prefer this approach but really don't know why.

  3. Jani Hartikainenon 27 Jan 2009 at 2:27 am

    Autocompletion with chaining will work in Zend Studio, NetBeans, possibly Eclipse PDT, if you provide correct phpdoc @return declarations for the methods you're chaining.

  4. cheap air jordanon 23 Jun 2010 at 8:00 pm

    The post of content is very interesting and exciting. I learned a lot from here.The content from simple to complex, so all of you can come in . No matter you want to see what can be found.By the way ,there are some websites is also very wonderful,you can go and see.such asbaby-namboos.com

RSS Feed
Subscribe by email
Follow me @ Twitter