PHP Method Chaining

With the help of php method chaining or PHP function chaining we can call more than one method or function of the class in single instruction. I have seen very rare implementation of php method chaining. If you have worked with Zend framework or magento(build on zend) you can get various example of method chaining. In this tutorial we will explore implementation of php method chaining. Following is a basic example of method chaining in php:
$a = new Order();
$a->CreateOrder()->sendOrderEmail()->createShipment();
Or if you want to call it in multiline script then you can make same call like below example
$a = new Order();
$a->CreateOrder()
->sendOrderEmail()
->createShipment();

In both of the above example object is calling more than one function or methods. This process is called method chaining in php. Method chaining works because our function or method of the class always returns object which further call another function. In this tutorial we will explore how to create class for method chaining and how to implement method chaining.

How to create class in PHP for method chaining

PHP method chaining only work because of the function or method of the class return an object. Let us create a prototype class for our above example of method chaining(refer above code block for method chaining)
class order
{
public $order_status;
public function createOrder()
{
//Apply logic to create order
$this->order_status = 'Order Created';
return $this;
}
public function sendOrderEmail()
{
//Apply logic for sending email to order
$this->order_status = 'Email Sent';
return $this;
}
public function createShipment()
{
//Apply logic for creating shipment
$this->order_status = 'Shipment Create';
return $this;
}
}

Please analyze above class methods which is used in our method chaining calls. Every class is returning $this object. So whenever you will call  createOrder function it will further give you return of the self class object so that you can call different function of same class and so on…

So using your function return, you can integently call more than one function of same class by your class object.

PHP method chaining can be implement with more that one class. And you can create chain of the method from more than one class. Following  is example of class for method chaining from multiple class.
Class xyz
{
function xyz_function()
{
return 1;
}
}
Class test
{
function test_function()
{
//Add your business log
$a = new xyz();
return $a;
}
}
$a = new test();
echo $a->test_function()->xyz_function();// will print 1.

How to implement chain of methods or function

If you have carefully observed all of the above example then hope have understanding of how php method chaining works. As we have already explored that how method chaining works. The basic reqirement for method chaining is to get only objects return from your method or function. Means if your function return object of any class then further you can call method of that object. Means If your called method return object of same class then in the chain next you can only call method of the self class. But if your object called function return object of another class then you can call method of the class which is return by your called method. Let us take  example of your order class again.
$a = new Order();
$a->CreateOrder()
->sendOrderEmail()
->createShipment();

Now look at class order:
class order
{
public $order_status;
public function createOrder()
{
//Apply logic to create order
$this->order_status = 'Order Created';
return $this;
}
public function sendOrderEmail()
{
//Apply logic for sending email to order
$this->order_status = 'Email Sent';
return $this;
}
public function createShipment()
{
//Apply logic for creating shipment
$this->order_status = 'Shipment Create';
return $this;
}
}

Here you can observe that every method is returning $this. So your next method or function of the chain will be from the same class.


Now let us take an example of test and xyz class.

class order
{
public $order_status;
public function createOrder()
{
//Apply logic to create order
$this->order_status = 'Order Created';
return $this;
}
public function sendOrderEmail()
{
//Apply logic for sending email to order
$this->order_status = 'Email Sent';
return $this;
}
public function createShipment()
{
//Apply logic for creating shipment
$this->order_status = 'Shipment Create';
return $this;
}
}

Please analyze above class methods which is used in our method chaining calls. Every class is returning $this object. So whenever you will call  createOrder function it will further give you return of the self class object so that you can call different function of same class and so on…

So using your function return, you can integently call more than one function of same class by your class object.

PHP method chaining can be implement with more that one class. And you can create chain of the method from more than one class. Following  is example of class for method chaining from multiple class.
Class xyz
{
function xyz_function()
{
return 1;
}
}
Class test
{
function test_function()
{
//Add your business log
$a = new xyz();
return $a;
}
}

Here your test class function test_function is returning object of the class xyz. So if you are calling calling test_function by object of the class test then you can call xyz_function as next method of your chain. Below is the example:
$a = new test();
echo $a->test_function()->xyz_function();// will print 1.

Hope you php method chaining concept is clear now.

 

 

Comments

  1. shubhmanoj says

    Thanks for giving nice tutorials, actually I was looking form long time such kind of nice tutorials

  2. says

    Thanks for giving nice tutorials, actually I was looking form long time such kind of nice tutorials, and I like one more this in your tutorials you have provided examples to download that very nice…

    thanks from heart.

  3. CoursesWeb says

    Hi,
    To return the class instance in the static method (to chain it), you can use:
    return new self; .

  4. Nacojohn says

    am grateful for the easy explanation of your tutorials. it really helped!!! kudos!!!!!!!!!!!!!!!!!!!!!!!!

  5. shyamender says

    Thank dear it is really full of knowledge . please also give a view on how to use database using oops

  6. Rahul Anand says

    Such a nice stuff…. really liked it.. very easy explained and well structured.. thanks for sharing such a nice article with us.

  7. says

    Dear Ankur Kumar Singh i am very happy today you just gave me a basic knowledge of the big OOP which ive found very difficult to understand for many years now….may the most high punish u with more wisdom and blessings for your kindness

  8. musthafa says

    thanks a lot dude… i tried many times in other books and website for concept of class and objects…but i dint understand properly… yours is great i understood cleraly… great job….

  9. Tarun Raturi says

    i am new for oops in php so please tell me how can i learn oops in php with easy way thanks

Leave a Reply

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


six + five =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>