HomePHP Building a CodeIgniter Custom Library with Method Chaining
Building a CodeIgniter Custom Library with Method Chaining
Welcome to the seventh part of a 12-part series on the technique of method chaining in PHP 5. In this part, I will demonstrate how method chaining can be used to develop some core methods of a custom library for CodeIgniter.
In case you haven’t explored it in depth yet, method chaining is a useful programming methodology that permits you to build highly modular and compact APIs when using the object-oriented paradigm. This isn’t a proprietary feature of a specific programming language; however, it’s possible to implement it with relative ease in PHP 5.
Thus, if you’re a PHP developer who wants to master the basics of creating chainable methods while defining your own classes, then this series of articles might be the material that you’re looking for. In it you’ll find numerous examples that will show you how to apply the method chaining approach to building real-world applications.
Now that you’re well aware of the goal of these tutorials, it’s time to reexamine the topics that were treated in the previous article. In that part of the series I completed the development of a simple, useful MySQL abstraction class that had the ability to perform typical retrieval queries by means of a few basic chainable methods.
Despite the rather simplistic logic implemented by the methods of this class, this particular example hopefully demonstrated how to use chainable methods for building a PHP program that can be used under “real conditions.”
However, as I said at the beginning of this series, my goal here is to illustrate how this approach can be implemented for creating applications that can be used in production environments. So, with that premise in mind, in the next few lines I’m going to show you in a step-by-step fashion how to build a custom library for the popular CodeIgniter framework. The library will allow you to perform the most common database operations without having to create multiple models.
Also, some of the methods that will be implemented within this application will rely partially on the great DataMapper library developed by Simon Stenhouse at http://stensi.com/datamapper, so I’d like to give the corresponding credit to him. Of course, if you need to get a full-featured ORM for CodeIgniter, I recommend you to give the library a try here (http://stensi.com/datamapper/pages/download.html). It’s really worthwhile.
Finally, a word of warning is in order here before you continue reading: the library will be pretty huge, with plenty of modular chainable methods, so I suggest that you arm yourself with patience and willpower to understand how each method is going to work.
Having clarified those points, it’s time to start building this custom library for CodeIgniter by using the functionality provided by method chaining. Let’s do it right now!