PHP
  Home arrow PHP arrow Page 2 - 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 different interface implementers: defining the “MySQL” class
    ( Page 2 of 4 )

    In order to work with different interface implementers, what I shall do is define two simple classes: a MySQL wrapping class and result-processing class, both aimed at working with MySQL data. Next, I’ll make the second class an implementer of the “HTMLRenderer” interface, so it’ll be capable of delivering HTML-formatted result sets through the specific definition of the “toHTML()” method. Now, objects of different types will be using the same interface.

    Let’s start by showing the “HTMLRenderer” interface, and then list the code for the “MySQL” wrapping class. Here are the definitions for both programming structures:

    // interface HTMLRenderer
    // defines generic method toHTML()
    interface HTMLRenderer {
        public abstract function toHTML();
    }

    // class MySQL
    class MySQL{
        private $conId; // connection identifier
        private $host; // MySQL host
        private $user; // MySQL username
        private $password; // MySQL password
        private $database; // MySQL database
        // constructor
        public function __construct($options=array()){
            // validate incoming parameters
            if(count($options)<4){
                throw new Exception('Invalid number of connection
    parameters');
            }
            foreach($options as $parameter=>$value){
                if(!$parameter||!$value){
                    throw new Exception('Invalid connection
    parameters');
                }
                $this->{$parameter}=$value;
            }
            // connect to MySQL
            $this->connectDB();
        }
        // connect to MYSQL server and select database
        private function connectDB(){
            if(!$this->conId=mysql_connect($this->host,$this-
    >user,$this->password)){
                throw new Exception('Error connecting to the
    server');
            }
            if(!mysql_select_db($this->database,$this->conId)){
     throw new Exception('Error selecting database');
            }
        }
        // perform query
        public function query($query){
            if(!$this->result=mysql_query($query,$this->conId)){
           throw new Exception('Error performing query '.$query);
            }
            // return new Result object
            return new Result($this,$this->result);
        }
    }

    Essentially, the above class is the same one that I presented in some of my previous articles, with minor modifications, thus I won’t stick you with boring details explaining its functionality. The only point worth mentioning is the definition of the “query()” method. As explained in my article “Object Interaction in PHP: Introduction to composition”, this method returns a new “Result” object, which is useful for separating the logic for handling tasks related to connecting to the server, selecting a database and running queries from the code for processing result sets.

    It’s precisely the class that returns result-type objects which will be turned into an “HTMLRenderer” interface implementer, causing these objects to define a “toHTML()” method for obtaining formatted result sets.

    Having taken a quick look at the “MySQL” class, let’s see how the pertinent interface is implemented by result-type objects.



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