আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

PHP Object Oriented Programming Part-3: PHP OOP Method Chaining

What is Method Chaining in PHP?

PHP OOP Method Chaining

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

<?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.

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:

<?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.

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:

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

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.

Leave a Reply

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