HomePHP The Basics of Implementing Adapter Objects with PHP
The Basics of Implementing Adapter Objects with PHP
You may have already encountered situations in coding PHP applications in which you would like to use inheritance, but could not for one reason or another. Fortunately, there is an alternative for these situations that can help you achieve the same result. In this first article in a two-part series, you'll learn about the adapter pattern.
During your life as PHP developer, Iím quite sure youíve been working with classes and objects for a while. That's very convenient for those times when you need to create a well-defined relationship between these classes, constructing web applications that are highly maintainable from the very beginning.
In addition to mastering the main pillars of the object-oriented programming paradigm, like inheritance and polymorphism, probably you have already started implementing some kind of advanced interaction across several objects that goes beyond the boundaries of deriving a few subclasses from the corresponding parent class.
Actually, while all this sounds good and seems to satisfy the most common requirements that must eventually be tackled when developing a certain PHP application, the truth is that sometimes using inheritance for creating specific objects inside a program isnít convenient. This could be because all the pertinent base classes werenít initially defined with a future expansion of their functionality in mind, or just because of simple ignorance.
In either case, there must be an alternative way to extend the functionality of a base class without directly using inheritance. Also, at the same time, this approach has to be flexible enough to allow extending this parent class in such a way that a newly-created object can fit the particular requirements of another one. Does this sound a bit confusing? Fear not, because the topic is really understandable.
To put things in a simpler way, say you have two predefined classes, where the first one must suit the demands of a second one, but as I said before, inheritance isnít the most viable method for performing the corresponding coupling of classes. Now, after reading the above lines, you should be asking yourself: how can this be done?
Fortunately for you and me, thereís a straightforward method for tackling the problem. It consists of creating what is called an ďadapterĒ class. As youíll see later on in this article, this adapting class will take the original one and modify --and eventually expand -- one or many of its methods to meet the specifications imposed by the destination class, all without appealing to the advantages of inheritance.
In crude terms, this method is known popularly as the ďadapterĒ pattern, and during this two-part series, Iíll be taking an in-depth look at it. In this way, you'll learn how to apply it within your own PHP applications and scripts.
The subject is really interesting, thus letís no waste more time in preliminaries and start learning more on how to create adapter objects with PHP. Letís do it together!