HomePHP 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.
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){ $this->mysql=$mysql; $this->result=$result; } // fetch row public function fetchRow(){ return mysql_fetch_array($this->result,MYSQL_ASSOC); } // count rows public function countRows(){ if(!$rows=mysql_num_rows($this->result)){ throw new Exception('Error counting rows'); } return $rows; } // count affected rows public function countAffectedRows(){ if(!$rows=mysql_affected_rows($this->mysql->conId)){ throw new Exception('Error counting affected rows'); } return $rows; } // get ID from last inserted row public function getInsertID(){ if(!$id=mysql_insert_id($this->mysql->conId)){ throw new Exception('Error getting ID'); } return $id; } // seek row public function seekRow($row=0){ if(!mysql_data_seek($this->result,$row)){ throw new Exception('Error seeking data'); } } // return HTML rendered result set public function toHTML(){ $this->output='<table>'; while($row=$this->fetchRow()){ $this->output.='<tr>'; foreach($row as $field=>$value){ $this->output.='<td>'.$value.'</td>'; } $this->output.='</tr>'; } $this->output.='</table>'; 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.