HomePHP Page 2 - The Basics of Implementing Adapter Objects with PHP
The basics of adapter objects - 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.
As you read in the introduction of this article, adapter objects can be useful for modifying the original structure of a specified class, but in this case without having to use inheritance to perform the corresponding structural changes.
Once the initial class have been altered via the mentioned adapter, an instance of it is usually passed in to another class, which expects the inputted object to conform the characteristics of a predefined model.
Of course, all this theory sounds interesting, but I’m sure that you want to see how the respective concepts can be translated into some pieces of functional PHP code. Therefore, my next step will be to set up a concrete example, where you can understand with minor hassles how an adapter class can be properly created.
First I’ll start developing the referenced example by coding the basic structure of a directory processor class with PHP 5. Then I'll create a simple adapter class to illustrate the complete procedure, aimed at “adapting” the signature of the mentioned directory processor.
Having said that, here is the definition for the new “AbstractDirectoryProcessor” class. As its name indicates, it defines the generic model of a directory handling object. Please look at the following class:
//define abstract 'AbstractDirectoryProcessor' class
abstractclass AbstractDirectoryProcessor{
private $dirPath;
abstract public function fetchDirContent();
abstract public function getDirInfo(); }
As illustrated in the above example, the initial definition that corresponds to the “AbstractDirectoryProcessor” class outlines the generic interface that will be implemented by all the eventual child classes derived from it.
In this particular case, two abstract methods called “fetchDirContent()” and “getDirInfo()” respectively have been appropriately declared, which suggest clearly what type of tasks will be performed by the eventual subclasses, once the respective methods have been concretely implemented.
As you might have guessed, the first method will be used for retrieving the contents of a given directory, while the second one will be tasked with displaying some useful information about the directory in question. This is quite simple, isn’t it?
Now that you learned how the previous “AbstractDirectoryProcessor” class was defined, it’s time to see how a concrete directory handling class can be created, based upon the structure of the corresponding parent class that you saw before.
That’s exactly the topic that will be treated in the following section, therefore click on the link that appears below and keep reading.