HomePHP Completing a Sample String Processor with Method Chaining
Completing a Sample String Processor with Method Chaining
In this third part of a 12-part series on method chaining, I complete the definition of the sample string processor class. This process will help reaffirm the concepts that you learned before regarding the definition and implementation of chainable methods in PHP 5.
Method chaining is a simple, yet powerful, programming approach that permits you to write classes whose methods can be easily linked with each other, thus building a modular and highly-compact API. Logically, as with other methodologies, chainable methods are not a proprietary feature of a particular programming language, but rather an approach that can be used for developing both client and server-side web applications, and desktop programs as well.
Fortunately, PHP developers like you also can take advantage of the benefits of using method chaining when working with the object-oriented paradigm. In this series of articles you'll find numerous examples that not only will introduce you to the basics of this approach, but will show you how to use it in the context of real-world applications.
Naturally, if you already read the two preceding tutorials of the series, then at this stage you have a clearer idea of how to build chainable methods in PHP 5. In those articles I developed a basic string processor class, which implemented some simple chainable methods for applying different filters to an incoming string.
In addition, it's fair to recall that this sample class defined a factory method (also chainable), which allowed you to create a singleton of the string processor without having to call its constructor directly.
However, it'd be pretty instructive to expand the existing functionality of the mentioned class by adding a few additional methods to it, which will also be completely chainable. In this third chapter of the series I'm going to finish developing this string processor class by incorporating into it some chainable methods that will be tasked with replacing characters in an inputted string with their equivalent HTML entities, and with for reversing their value as well.
Now, it's time to continue creating more examples on using method chaining in PHP 5. Let's jump in!