Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. The Basics of Implementing Adapter Objects with PHP
  2. The basics of adapter objects
  3. Defining a concrete directory processor class
  4. Seeing the adapter pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
December 06, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Introduction

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!



 
 
>>> 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: