Home arrow PHP arrow Method Chaining: Adding More Methods to the Chain

Method Chaining: Adding More Methods to the Chain

In this second part of a 12-part series on method chaining, I explain how to extend the functionality of the string processor class by adding to it a few simple chainable methods. I conclude this part by defining a factory method within the string processor class.

TABLE OF CONTENTS:
  1. Method Chaining: Adding More Methods to the Chain
  2. Adding more methods to the string processor class
  3. Adding another chainable method to the class
  4. Defining a chainable factory method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
October 26, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Method chaining is a programming approach that permits you to chain multiple methods of a class in one single step, to perform several tasks in the context of a specified application.

Logically, this approach can be used equally with various programming languages, and PHP 5 certainly is no exception. Therefore, if youíre a PHP developer who wants to learn the basic concepts that surround the definition and implementation of chainable methods using PHP 5, then in this group of articles youíll find the material that youíve been looking for.

And now that you've been introduced to the primary goal of this article series, itís time to review the topics that were covered in the last tutorial. In that article I proceeded to build a simple string processor class. It implemented a bunch of basic methods for performing some common tasks on an inputted string, such as removing unwanted tags from it, and upper casing and lower casing its characters.

Naturally, the most important thing to point out here is that despite the simplicity of the methods, they could be chained to each other, thus allowing us to use the classí API in a much more compact and tight way.

Nonetheless, the functionality of this sample class can be enhanced through the codification of other chainable methods. Whatís more, itís possible to define a static factory method, also chainable, that allows you to create instances of the class without directly calling its constructor.

Bearing in mind all of these interesting possibilities, in this second part of the series Iím going to demonstrate how to accomplish these tasks in a truly friendly fashion, as usual by coding some comprehensive hands-on examples.

Now, letís leave the preliminaries behind us and continue exploring the advantages of using method chaining in PHP 5. Letís get going!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: