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

Describing the logic - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

First off, a MySQL object ($this->mysql), along with a query resource ($this->result) are assigned as class properties within the constructor. Then, the usual methods for fetching, counting and seeking table rows are properly declared, as well as for getting the ID from the last inserted record. Here are the relevant methods for performing these tasks:

// 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');
    }
}

Of course the most noticeable modification introduced to the code is the addition of the “toHTML()” method. By following the requirements for working with interfaces, this method provides an explicit definition for getting a formatted result set. In this particular case, I’ve opted to return table rows within a simple <table> container, but you may want to change this and return a formatted result set using other tags.

The code below illustrates how table rows are returned within a (X)HTML table structure:

// 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;
    }
}

Now, an object instantiated from the above “Result” class will present a specific “toHTML()” method, useful for returning HTML rendered result sets. Thus, if you gently step back to the previous article of the series, you’ll see that whether working with (X)HTML widget objects or with result-type objects, both family types implement the same interface, by defining specifically the “toHTML()” method.

Throughout the correct usage of interfaces I’ve covered an important programming requirement often needed when working with different types of objects that implement the same set of methods. Certainly this is something that’s not possible using a common abstract parent class. As you can see, grouping objects within an application can be easily achieved by utilizing interfaces.

By this point, I’ve explained how a functional MySQL processing class can be easily turned into an implementer of the HTMLRendered interface, allowing us to group objects that belong to different types. Considering that now a result-set class is capable of retrieving formatted table rows, probably you can imagine that rendering object-oriented web pages is a quite straightforward process. Want to know more? Thus, join me to read the conclusions.

To wrap up

Over this third part of the series, I’ve provided you with all the sample classes that also implement the “HTMLRenderer” interface, in this way demonstrating how objects belonging to distinct family types may be defined for using a shared set of concrete methods (declared as abstract within the interface) even if they have nothing in common. Just study in detail the underlying concept behind user-defined interfaces, and you’ll probably come up with brand new uses to be implemented in your own applications.

In the next part of the series, I’ll put the pieces together, by including the (X)HTML widget classes along with the MySQL-processing classes into a full-featured example, in order to illustrate how both types of objects can be implemented through a bridging class, for generating object-oriented web pages. So, get ready for the next coding challenge!



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