PHP
  Home arrow PHP arrow Page 3 - User-defined Interfaces in PHP 5: Turning MySQL Classes into Interface Implementers
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

User-defined Interfaces in PHP 5: Turning MySQL Classes into Interface Implementers
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2006-01-02


    Table of Contents:
  • User-defined Interfaces in PHP 5: Turning MySQL Classes into Interface Implementers
  • Working with different interface implementers: defining the “MySQL” class
  • Working with result sets: implementing the “HTMLRenderer” on result-type objects
  • Describing the logic

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    User-defined Interfaces in PHP 5: Turning MySQL Classes into Interface Implementers - Working with result sets: implementing the “HTMLRenderer” on result-type objects
    ( Page 3 of 4 )

    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.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek