PHP
  Home arrow PHP arrow Page 4 - 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 - Describing the logic
    ( Page 4 of 4 )

    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
     

       

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek