HomePHP Page 4 - Abstracting Database Access Using Polymorphism with Objects in PHP 5
Demonstrating the functionality of Polymorphism - PHP
Polymorphism is an object-oriented programming concept that can be difficult to understand. This article, the first of a three-part series, walks you through the basics of Polymorphism so that you can make use of it in your own applications. The specific example in this article will show you how to use it with different database systems.
In this section I'm going to create a couple of hands-on examples, which hopefully will help you understand more easily the convenience of working with polymorphic objects.
This being said, here is the first code sample. It demonstrates how easy it is to interact with MySQL using the database abstraction layer that I developed earlier. The corresponding code listing is as follows:
(example using MySQL database server) try{ // use Factory pattern and Polymorphism to work with MySQL $factoryInstance=new DBFactory(); $db=$factoryInstance->createDB ('MySQL','host','user','password','database'); $db->query('SELECT name,email FROM users'); while($row=$db->fetchRow()){ echo $row['name'].' '.$row['email'].'<br />'; } echo 'Number of rows returned by the query :'.$db->countRows (); /* displays the following user1 user1@domain.com user2 user2@domain.com user3 user3@domain.com user4 user4@domain.com user5 user5@domain.com user6 user6@domain.com user7 user7@domain.com user8 user8@domain.com user9 user9@domain.com user10 user10@domain.com Number of rows returned by the query :10
As shown above, accessing the MySQL database server is extremely simple by using Polymorphism. The previous example uses the "query()", fetchRow()" and "countRows()" methods to perform some common tasks associated with MySQL, such as running queries and fetching/counting rows. But this advantage is even more clear if you look at the following example. It uses the same methods, but this time accesses the SQLite database system.
Assuming that a sample SQLite database has been populated with basic data, the respective code listing is as follows:
(example using SQLite database system)
try{ // use Factory pattern and Polymorphism to work with SQLite $factoryInstance=new DBFactory(); $db=$factoryInstance->createDB('SQLite'); $db->query('SELECT name,email FROM users'); while($row=$db->fetchRow()){ echo $row['name'].' '.$row['email'].'<br />'; } echo 'Number of rows returned by the query :'.$db->countRows ();
/* displays the following:
User1 user1@domain.com User2 user2@domain.com User3 user3@domain.com Number of rows returned by the query :3
Definitely, after examining the previous example, you'll have to agree with me that building a database abstraction layer using polymorphic objects is by far a much better approach than the one shown in the beginning of this tutorial.
Naturally, I'm not saying here that you have to reinvent the wheel and write your own abstraction layers; there are many excellent packages available on the web. But I do want you to understand the advantages of using Polymorphism when building your PHP applications, since the concept can be rapidly extended to other areas.
Final thoughts
That's all for the moment. In this first article of the series, I demonstrated in a friendly way how to use polymorphic classes to build a simple database abstraction layer. Nevertheless, this is only the beginning, since in the next part I'm going to teach you how to use polymorphism to create dynamic web pages. You won't want to miss it!