Back to Top

How to use PHP Method Chaining?

Method Chaining in PHP

Method Chaining is the new feature of object-oriented approach introduced in PHP5.Today I am going to explain how to use method chaining in PHP5.

Method Chaining is like to call method in chain means we can call more than one methods using single instance.

Let’s see the SYNTAX of Method Chaining:

Above Syntax explains that method1() will return an object and method2() will send the result of method1() and then method2() will pass the return value to method3().

To implement Method Chaining, You must have to return object value from methods.The returned object not to be the same one from which the method is being called.

If you have worked with CodeIgniter Framework, you must have idea about the method chaining.

Use Object:

Here, I have created one class for resizing an image and created an object of the class and called all methods in chain which will resize image and save into folder.

That’s it.It is important to note that the method chaining require to return the object and can’t return any other values then the object.

Also Read:

Static Keyword in PHP
Constructor and Destructor in PHP
Type Hinting in PHP5

Hope this article helpful to you.As always, thanks for reading an article. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

The Readers’ Poll – August 2013

Posted on 11 years ago

Bhumi

how to shake a div using jQuery

Posted on 11 years ago

Bhumi