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:
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.
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.
Now look at class order:
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.
$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.
No comments:
Post a Comment