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

Working with different interface implementers: defining the “MySQL” class - 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

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
 

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: