HomePHP Building a MySQL Abstraction Class with Method Chaining
Building a MySQL Abstraction Class with Method Chaining
In this fourth part of a 12-part series on method chaining, I start building a basic MySQL abstraction class that implements a few straightforward methods. Of course, the methods can be easily chained to each other, which permits us to build different parts of a SELECT statement through a truly compact and readable API.
Put it in a simple way, method chaining is a handy programming methodology that allows you to create classes whose methods can be easily linked to each other, thus building a highly compact API.
Building chainable methods is not a proprietary approach supported by a particular programming language. It can be applied to multiple languages. In the case of PHP 5, it is very simple to implement, even if you have an average background in the object-oriented paradigm.
Therefore, if youíre a truly passionate PHP developer who wants to learn the basics of building chainable methods and how to use them in real-world applications, then donít hesitate any longer; start reading this series of articles now!
Right now I'm going to review the topics that were discussed in the previous part. In that tutorial I finished building a simple string processor class, which could apply different filters to an incoming string.
Of course, apart from the simplistic logic implemented by the methods of this sample class, the most important aspect to stress here is that these methods were completely chainable, thus making it possible to invoke them by writing only a few lines of short, tight code.
However, this class was built only as an introductory example, handy for learning the fundamentals of applying method chaining in PHP 5. But this approach can be used for developing more useful applications. So, with that idea in mind, in the next few lines Iím going to explain how to create a simple MySQL abstraction class whose methods will be chainable.
Now itís time to see how to use method chaining to abstract access to MySQL. Letís get going!