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.

 

 

 

50 Responses to PHP Method Chaining

  1. rbutterton says:

    Thank you for this tut! Great Stuff!

  2. Excellet Tutorials….

  3. dinesh sharma says:

    Yes ! respected sir…

    I am very happy with you tutorials method…

    keep it up…

  4. shubhmanoj says:

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

  5. shubhmanoj 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.

  6. CoursesWeb says:

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

  7. Very good basic tutorials

  8. vignesh.R says:

    hi
    thanking u
    esay understanding oops with php

  9. Pardeep Panchal says:

    good for beginners………….

  10. Nagababu says:

    I am very happy with you tutorials method…

  11. upendra says:

    Very Nice tutorials, keep it up

  12. Mayu says:

    Awesome stuff bro. Thanks alot.

  13. Darko says:

    Thank you very much, It helped me

  14. Riaz says:

    thank’s so much sir I am so much grateful to you

  15. shahid says:

    Excellent tutorial for oops thanks a lot

  16. Meenal says:

    Awesome

  17. prashant mestry says:

    Very knowledge gaining Friend….

  18. Nacojohn says:

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

  19. Sujana says:

    good stuff ….

  20. kiran says:

    Vary nice article. Tthank you very much friend

  21. shyamender says:

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

  22. Arvind says:

    wooo realy nise very gud.
    Thanks

  23. Neeraj says:

    good tutorial

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

  25. Rajesh Tomar says:

    Thanks a lot. Really really very good explanation. So simple and tons meaningful. :-)

  26. aman kamboj says:

    Good sir, keep it up…

  27. shrikant says:

    very good sir

  28. reshma says:

    good tuitorial, very useful

  29. virendra rastogi says:

    Pretty good stuff for study purpose….

  30. khan2701 says:

    Excellent tutorial sir………thanks a lot

  31. HENRY 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

  32. Karthick says:

    Excellent Tutorial

  33. Ko Soe says:

    Excellent tutorial!! Thank a lot.

  34. girish says:

    Nice tutorial… I learn basic concepts in this tutorial. Thanks once again….. :)

  35. mountain says:

    thank so much author

  36. Alim Hali says:

    Very Nice Tutorial..

  37. ravi says:

    its very use full good one…

  38. Puneet says:

    It is easy for understand.Thanksssss…..

  39. 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….

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

  41. Farzina Sahin says:

    This article is very useful and easy to understand for getting OOP concept. Thanks a lot !

  42. how easily describe each lines..really nice tutorial

  43. Chirag Sidhdhapara says:

    very nice tutorial

  44. punam says:

    Thanks a lot….! this notes are very easy & meaningful to understand. Again thanks a lot……..

  45. udy says:

    nice…..i think you shud change the code-highlither

  46. anbu says:

    Very very……………nice

Leave a Reply