February 21, 2019
Hot Topics:

PHP 5 OOP: Interfaces Abstract Classes and the Adapter Pattern

  • May 5, 2006
  • By Adam Delves
  • Send Email »
  • More Articles »

Look at a class in Listing 4 that implements the database interface. You should recall that I mentioned the adapter pattern earlier. This is an example of the adapter pattern, which is used by programmers in order to adapt one API. The API you are adapting from could be another object-based API or as being done here, an adaptation from a modular API. If you want to read more about the adapter pattern, you can find a more detailed explanation and examples here.

Notice how the escape_string() method is included as a static method. This method does not require an active connection to a database and should not require and instance of any object which implements the DB interface. In my opinion, this is the single most important method of any database implementation; a poorly implemented escape string method could make your applications vulnerable SQL injection.

Listing 4: Implementing the database interface

class MySqlDB implements DB 
        private  $link; 
        public function connect($server='', $username='', $password='', $new_link=true, $client_flags=0) 
            $this->link = mysql_connect($server, $username, $password, $new_link, $client_flags); 
        public function errno() 
            return mysql_errno($this->link); 

        public function error() 
            return mysql_error($this->link); 

        public static function escape_string($string) 
            return mysql_real_escape_string($string); 

        public function query($query) 
            return mysql_query($query, $this->link); 
        public function fetch_array($result, $array_type = MYSQL_BOTH) 
            return mysql_fetch_array($result, $array_type); 

        public function fetch_row($result) 
            return mysql_fetch_row($result); 
        public function fetch_assoc($result) 
            return mysql_fetch_assoc($result); 
        public function fetch_object($result) 
            return mysql_fetch_object($result); 
        public function num_rows($result) 
            return mysql_num_rows($result); 
        public function close() 
            return mysql_close($this->link); 

You'll notice that there are many more mysql functions than methods that are adapted in the interface in listing 3. However, this small subset of functions is sufficient to meet the needs of most applications requiring trivial data storage and retrieval. The additional functions can be implemented and I have done this in the attached example file, you may also choose to add additional functionality to the class and the interface.

Listing 5: Creating a database class

$db = new MySqlDb; 
    $db->connect('host', 'username', 'password'); 
    $db->query('use users'); // we could also use $db->select_db here but it is not consistent with the interface 

    $result = $db->query("SELECT username FROM users"); 
    while($row = $db->fetch_assoc($reuslt)) { 

As shown in listing 5, you can now create a class for each database you want and as long as it implements the DB interface, switching from one to another is as easy as changing one line of code:

$db = new MsSqlDb; 


In this first article, you've seen how to create an abstraction layer to access a database. Using this, you saw how creating an interface helps you to isolate your application so that you can easily switch from one database to anther without rewriting your own applications.


You can download phpcodefiles.zip here.

About the Author

Adam Delves is a university student and web programmer from the UK who is studying computing. He has been a PHP programmer for over 3 years and now runs two small websites and writes articles for PHP builder.com.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date