Home arrow PHP arrow Page 3 - The Role of Interfaces in Applying the Dependency Injection Design Pattern

Defining a database access interface - PHP

Welcome to the sixth part of a series covering the dependency injection pattern. In this part, I build a PHP 5-based application that can work seamlessly with MySQL and SQLite. It will feature a simple interface and a MySQL driver.

TABLE OF CONTENTS:
  1. The Role of Interfaces in Applying the Dependency Injection Design Pattern
  2. Review: implementing dependency injection via a setter method
  3. Defining a database access interface
  4. Building a MySQL database driver
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
October 20, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I expressed at the beginning of this article, one typical situation where dependency injection is used at its most efficient is when it comes to building applications that need to work with multiple database systems.

To demonstrate this concept as clearly as possible, I’m going to start developing a PHP 5 program that will be capable of using both MySQL and SQLite to perform CRUD operations against a selected database. So, the first step that must be taken is defining a simple interface that will declare at an abstract level the methods that will be implemented by the respective database drivers.

Now that I have explained that, please examine the definition of the following interface, which not surprisingly is called “DbHandler.” Here it is:

// define 'DbHandler' interface

 

interface DbHandler

{

public function __construct($host, $user, $password, $database);

 

public function query($query);

 

public function fetch();

 

public function count();

}

Were you expecting to see longer code? Sorry to disappoint you if you were, but I’d like to keep the structure of the above “DbHandler” interface pretty simple and uncluttered (after all, that’s the idea behind defining an interface, right?). As you can see, it declares four public methods that will come in handy for connecting to the specified database and running queries, as well as for fetching and counting rows in result sets.

So far, there’s nothing unusual about the way that this interface has been defined. So, it’s time to start out building the first database driver class. This clase will be responsible for implementing the methods declared by the interface to work with MySQL.

The definition of this class will be shown in the last section of this tutorial. Therefore, go ahead and read that segment.



 
 
>>> 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: