Home arrow PHP arrow Page 3 - User-defined Interfaces in PHP 5: Turning MySQL Classes into Interface Implementers

Working with result sets: implementing the “HTMLRenderer” on result-type objects - PHP

Welcome to the third part of the series “User-defined interfaces in PHP5.” In four parts, this series explains the use of interfaces in PHP5, highlighting their advantages and illustrating their implementation in real applications.

  1. User-defined Interfaces in PHP 5: Turning MySQL Classes into Interface Implementers
  2. Working with different interface implementers: defining the “MySQL” class
  3. Working with result sets: implementing the “HTMLRenderer” on result-type objects
  4. Describing the logic
By: Alejandro Gervasio
Rating: starstarstarstarstar / 10
January 02, 2006

print this article



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
class Result implements HTMLRenderer{
    private $output; // dynamic output
    private $mysql; // instance of MySQL object
    private $result; // MySQL result set
    // constructor
    public function __construct($mysql,$result){
    // fetch row
    public function fetchRow(){
        return mysql_fetch_array($this->result,MYSQL_ASSOC);
    // count rows
    public function countRows(){
 throw new Exception('Error counting rows');
        return $rows;
    // count affected rows
    public function countAffectedRows(){
 throw new Exception('Error counting affected rows');
        return $rows;
    // get ID from last inserted row
    public function getInsertID(){
 throw new Exception('Error getting ID');
        return $id;
    // seek row
    public function seekRow($row=0){
 throw new Exception('Error seeking data');
    // return HTML rendered result set
    public function toHTML(){
            foreach($row as $field=>$value){
        return $this->output;

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.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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