PHP Object Oriented Programming Part-3( PHP OOP Method Chaining)

What is Method Chaining in PHP?

When many methods are called in a single instruction, in PHP’s term it is called Method Chaining. Let’s explain it a little easier, suppose you have a class named Person, where you have two private property named $name and $age. And there are two methods named setName() and setAge() to set value in $name and $age property. And lastly, to show the value of $name and $age property, there is a getResult() method. And suppose the name of our object is $person. Now if we do not want to call these three methods differently, instead we want to call the object by just calling it in a single instruction, then we can write like this $person->setName(“Masud Alam”)->setAge(33)->getResult(). This method is called Method Chaining in PHP’s terminology. Each method in class in Method Chaining, that is, the method of the class returns the object of that class. For Method Chaining, instead of writing value return in class, we have to write return $this;.

Example 1: General class and its results

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
 class person
 {
    private $name="";
    private $age="";
 
    public function setName($name="")
    {
      $this->name=$name;
    }
    public function setAge($age="20")
    {
      $this->age=$age;
    }
    public function getInfo()
    {
      echo "Hello, My name is ".$this->name." and my age is ".$this->age." years.";
    }
}
 
$person = new person();
  $person->setName("Masud Alam");
  $person->setAge("33");
  $person->getInfo();
?>

Result

Hello, My name id Masud Alam and my age is 33 years.

Note that we have call setName() and setAge() method separately to set value in $name and $age property of the class named person. And lastly, called the getResult() method to show value of $name and $age property.

READ  How To Work With Codeigniter Database Caching

Uses of Method Chaining:

If you want to convert the class used in example 1 to Method Chaining you have add return $this line after setName() and setAge() method. See the example below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
class person
{
  private $name="";
  private $age="";
 
  public function setName($name="")
  {
    $this->name=$name;  
    return $this;
  }
  public function setAge($age="20")
  {
    $this->age=$age;
    return $this;
  }
 
  public function getInfo()
  {
    echo "Hello, My name is ".$this->name." and my age is ".$this->age." years.";
  }
}
 
$person = new person();
  $person->setName("Masud Alam")->setAge("33")->getInfo();
 ?>

Result

Hello, My name id Masud Alam and my age is 33 years.

Explanation: Notice that we return $this pseudo-variable between setName() method and setAge() method. And we know that “$ this” pseudo-variable originally contained the object of the current class. Let’s first come to $person->setName(‘Masud Alam’), Its job is to call setName() Method and set Masud Alam to name property as value. Then setName () method will return $this, That means it will return the Object person.

Next let’s go to ->stAge(’33’), As we got the chain object from the previous setName() method, So, with that person object, we can call setAge(33) and set age 33 as value in age property. Again here we get $this as return, Similarly, here we will get chain as return to our object person.

Lastly by ->getInfo() method we get chain result for the previous series.

READ  Describe Factory Design Pattern in PHP Technology?

Static Method Chaining

Static Method Chaining is similar to General Method Chaining. But here for each static method, instead of writing return $this;, we have to write return new static;. See the example below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
class oop{
    public static $val;
     
    public static function init($var){
        static::$val=$var;
        return new static;      
    }
 
    public static function add($var){
        static::$val+=$var;
        return new static;
    }
 
    public static function sub($var){
        static::$val-=$var;
        return new static;
    }
 
    public static function out(){
        return static::$val;
    }
 
}
 
echo oop::init(10)->add(2)->sub(5)->out(); //Result: 10+2-5=7
echo "<br>";
echo oop::sub(2)->out(); //Result: 7-2=5;