PHP Page 3 - User-defined Interfaces in PHP 5: Turning MySQL Classes into Interface Implementers |
Making result-type objects implement the “HTMLRenderer” interface is a really straightforward task. But, before listing the code for the “Result” class, let’s analyze for a moment the benefits of having these objects sharing the same interface. Suppose that a web program accesses the database layer, then obtains some type of result set, and finally sends back the data to the presentational layer, which generates the user interface. Many times it’s desirable to have a result-processing class that is capable of returning completely formatted output, for passing to the presentational logic. Since this functionality is fairly easy to introduce into the class as a new method, the application is much more flexible because it presents at least two modes of rendering database information: either by pulling down the data directly as a formatted package, or as crude content for being transferred to the presentational layer. So, with reference to having a result-processing class acting as an implementer of the “HTMLRenderer” interface, below is its definition: //class Result Certainly, the “Result” class should be familiar to anyone who has worked with classes that break down the logic for handling result sets. If this concept is rather foreign to you, again I recommend that you read my article about object composition in PHP. Now, you can turn your attention to the source code of the class to see how it implements the “HTMLRenderer” interface. Definitely, the first thing to point out is the use of the “interface” keyword specified after the name of the class, like this: class Result implements HTMLRenderer As you probably remember, a class may implement any number of interfaces at the same time, so when working with multiple interfaces, they should be properly declared in turn after the class name. Having defined the “Result” class as an implementer of the “HTMLRenderer” interface, let’s describe its logic.
blog comments powered by Disqus |