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:
/** * 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.




