PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Advanced OOP With SPL In PHP 5

Creating Our Interface

To create a portable application, you must determine how the application will interact with the database. In this solution, you will create an interface which purpose is to provide a contract to the application, stating that no matter what database you are using, the integration methods will always take the same parameters and return the same values.

To start, create a file called IDataBaseBindings.php. The contents will look something like this:

Listing 1 IDataBaseBindings.php
<?php
    /**
     * IDataBaseBindings interface
     *
     * This interface contains the standarized implementation point for all
     * database methods.
     *
     * @author  Kevin McArthur
     * @depends UnbufferedAssociativeResultSet
     */
 
    interface IDataBaseBindings
    {
        /**
         * getCustomerDetails
         *
         * Returns an UnbufferedAsociativeResultSet containing all
         * detailed information about a user from the userDetails table.
         *
         * @param   int     $userid     A user identification number like 12345
         * @return  UnbufferedAsociativeResultSet    All relevant data from userDetails
         * @throws  ReadOnlyException
         */
        public function getCustomerDetails($userid);
    }
?>

The above code creates an interface called IDataBaseBindings which your implementing class will fulfill to create the database abstraction.

By using an interface like this, you could provide a different implementing class for each database you wanted to use. PostgreSQL’s implementer could use the CIDR data type, while in MySQL you would do the Boolean math to resolve the info.

This is just one easy example, but there are many cases where you will want to leverage the power of a specific database for performance reasons, especially when dealing with stored procedures, prepared queries and large volumes of data.

In This Article