There is no way to dispute the unmatched programming power and flexibility that PHP (Hypertext Preprocessor) brings to the table. Every web application development company today, engages in PHP-based solution development and offers its team of PHP programmers for hire to clients. For a technology with such high demand, every developer in the PHP community waits with bated breath for the latest release that comes with new features to allow them to break previously set development benchmarks. When PHP 5 released, the wide array of features that it possessed caught the attention of every PHP enthusiast. Its prime focus being on object oriented programming, PHP 5 also offered a totally revamped MySQL extension and a variety of seamless XML tools. In this article, we will deal with one of those features called method chaining.
Introduced under the umbrella of the OOP (Object Oriented Programming) improvement endeavor, method chaining allows you to link methods and return objects. This provides a novel approach towards object and method handling in PHP. While returning objects, PHP 5 offers the freedom of returning an object that may not necessarily be the one from which the method is called. This comes in handy when creating user classes, creating object from those classes and calling a method to return the output. All that needs to be done is the addition of a “return $this” to the end of the methods. Now the methods return the objects of the class where they are inserted. A typical function definition within a class would look like this:
public function setModel($model =””)
public function setMake($make =””)
The display function for such a linked method can be as shown below:
public function car()
echo “This is a “.$this->make” “.$this->model.””;
And now, the linked method call:
The output for this would be – “This is a 1960 Volkswagen Beetle.”
How does the chained method call work? When the $classname->setMake(“1960”) is called, it sets the model of the car and returns the object of the class. Same goes for the setModel method as it is chained to the method above. Finally when the car() function is called, it prints out the make and model of the car.
Essentially, if return $this is used then methods can be easily chained together. This greatly enhances code readability and reduces redundancy. This will also eliminate unnecessary code repetition. This and many more of such powerful features are provided by PHP 5. Not only does it reduce the amount of effort invested by developers into solution development, it also simplifies post-release maintenance and debugging. We are a web application development company providing cost-effective PHP MySQL application development. With a team of PHP5 developers and experts, we have broken through many programming barriers and have armed ourselves with the latest in development technologies. We offer our PHP programmers for hire so that your solution can benefit from our strategic expertise. If you wish to leverage the power of PHP5, get in touch with us.