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.

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

Most Popular Posts

Table layout using DIV tags

Posted on 11 years ago

Bhumi

Type Hinting Callable Functions in PHP

Posted on 6 years ago

Bhumi

Custom Directives in Angular JS

Posted on 8 years ago

Bhumi