HomePHP Abstracting Database Access Using Polymorphism with Objects in PHP 5
Abstracting Database Access Using Polymorphism with Objects in PHP 5
Polymorphism is an object-oriented programming concept that can be difficult to understand. This article, the first of a three-part series, walks you through the basics of Polymorphism so that you can make use of it in your own applications. The specific example in this article will show you how to use it with different database systems.
Since the introduction of a highly improved object model in PHP 5, developing object-based applications with the latest version of this neat scripting language has been a greatly simplified process. This is naturally due to the implementation of diverse and powerful object-oriented features, such as visibility of class members, type hinting and exceptions, to name a few.
However, while it should be admitted that the aforementioned improvements indeed facilitate working with classes and objects in a straightforward way, it's also true that building object-oriented applications can be a hard-to-master process. Most of the time this is because of inadequate knowledge of the main pillars of the object-oriented paradigm.
In this case, you'll possibly know that I'm talking about Inheritance and Polymorphism. But what can be wrong with putting these theoretical concepts into practice, after all? Well, if you've been developing object-based programs for a while, then you've probably been consciously defining parent classes here and there. Logically you've also created numerous subclasses to perform concrete tasks.
I have to admit that Inheritance is rather easy to grasp. It can be applied successfully with only a decent background in object-oriented programming. Nonetheless, there's a part of this schema that very often is completely omitted, or misused, in the best case. As you may have guessed, I'm speaking of Polymorphism, certainly a concept that presents some abstract aspects. Consequently, it's much harder to apply than Inheritance, at least in a real-world context.
But take a deep breath and think twice. Actually, in the arena of object-based programming it isn't too difficult to understand how objects that belong to the same family can have different behaviors, even when they're using the same methods. And besides, there's an additional question that needs to be answered: how can this feature be used to create more efficient PHP applications?
As you can see, questions come up very quickly when it comes to taking advantage of Polymorphism in PHP. This implies that the subject in question deserves a closer look. Therefore, keeping in mind the interest you might have in how to get the most out of polymorphic objects in PHP 5, in this three part series, I'm going to take a deeper look at some concrete situations where Polymorphism can assists in developing more robust and efficient object-based applications.
More specifically speaking, in this first installment of the series, I'll be explaining how to implement Polymorphism to achieve a high level of abstraction when accessing different database systems. Subsequent articles will cover the use of polymorphic objects to build dynamic web documents and validate user-supplied input.
So, having established the topics that I plan to treat in this group of articles, let's get started now!