PHP Method Chaining Explanation with Example

Hello developers, hope you are doing well. Today in this tutorial i will discuss about PHP method chaining. So in this tutorial you will learn from scratch about how we can call multiple method in php using php method chaining.

Method chaining interface allows you to chain method calls, which results in less typed characters when applying multiple operations on the same object.

Using method chaining

$programmer->born()->eat()->sleep()->code()->die();

 

But without method chaining

$programmer->born();
$programmer->eat();
$programmer->sleep();
$programmer->code();
$programmer->die();

 

See that method chaining gives us more readable code. So in this example we can see how we can write and print data using php method chaining. 

Simple php method chaining example:

class Person{
  private $_name;
  private $_sex;
  private $_age;
  private $_height;
  private $_weight;

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

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

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

  public function height($h){
    $this->_height = $h;
    return $this;
  }

  public function weight($w){
    $this->_weight = $w;
    return $this;
  }
  public function save(){
    $properties = get_object_vars($this);
    $str = '';
    foreach($properties as $property){
        $str .= $property.' ';
    }
    return $str;
  }
}

 

Now we can call the methods on the object of Person like

$p = new Person();
$res = $p->name('Sonia')->sex('Female')->age('30')->height('5.8')->weight('51')->save();
echo $res; # Sonia Female 30 5.8 51

 

As you see, by returning $this from the class methods after setting class properties, we’ve now achieved method chaining which can be used on the object instance, which not only reduces the amount code needed but also improved the readability of the code.

So now you know that method chaining is a useful featured expecially for procedural sequential API. So you have to keep in mind that the Chainable class is a simple wrapper for making proxy call to class methods and returning the instance reference permitting method chaining.

Now see another example

For instance, consider the Test class which has three methods :

class Test {
    function foo(){ echo "FOO"; }
    function baz(){ return "BAZ"; }
    function bar(){ echo "BAR"; }
}

 

and this example use :

$test = new Test();

$test->foo();
$value = $test->baz();
$test->bar();

echo "\n",'The value was : ',$value;

/* Output:
    FOOBAR
    The value was : BAZ
*/

 

Consider now the opportunity to chain methods, this is what we want to achieve :

$test->foo()->bar();

 

For doing so, without modify the Test class, we must wrap it in another proxy class and redirect all calls to the wrapped instance.

class Chainable {
    private $instance = null;
    public function __construct(){
        $pars = func_get_args();
        $this->instance = is_object($obj=array_shift($pars))?$obj:new $obj($pars);
    }

    public function __call($name,$pars){
        call_user_func_array([$this->instance,$name],$pars);
        return $this;
    }

}

 

Now we can wrap Test with Chainable and chain methods calls :

$test = new Chainable('Test');

$test->foo()->baz()->bar();

/* Output:
    FOOBAR
*/

 

Read also : Refactor Your Conditions to a Method in PHP

 

Hope it can help you.