Login

Creating a MySQL Abstraction Layer with Bridge Classes in PHP 5

If learning how to incorporate creational design patterns into your own PHP 5 applications is a high-priority topic for you, then this set of comprehensive articles might be what you’re looking for. Welcome to the concluding installment of the series “Using Bridge Classes with PHP 5.” These tutorials will teach you how to implement the bridge pattern in PHP by providing you with many hands-on examples that can be included in your existing and future web projects.

Before I move on and pay attention to the subject of this final article, first I’d like to refresh, at least quickly, some topics that were covered with detail in the preceding part of the series. As you’ll certainly recall, in the previous tutorial I extended the implementation of the bridge pattern with PHP 5, by defining a bunch of straightforward classes, which when used in conjunction, followed the principles dictated by the mentioned pattern.

Speaking more specifically, I created a bridge class called “BridgeDataValidator” which defined the generic structure of a data validation object. This is handy for checking different types of user-provided input. However, the most relevant aspect of this schema rested on the capacity offered by the referenced class to create distinct data validator objects. These objects implemented concretely the required logic for checking numbers, alphabetic values, and email addresses.

Naturally, the data validation objects were conceived as independent structures, which resulted in the complete decoupling of the “BridgeDataValidator” class from its own implementation. Of course, if you’re familiar with how the bridge pattern works, then you’ll realize that all the classes that I developed in the previous tutorial precisely followed that model.

Now that I hopefully refreshed the topics that were treated in the preceding article of the series, it’s time to focus your attention on the current one. The question that comes up now is the following: is there anything else that can be said about the bridge pattern? Of course there is! In this final tutorial I’m going to show you how to create a highly expandable abstraction layer that will allow you to work with MySQL using different libraries. This sounds interesting, right?

Having introduced the subject of this installment, let’s leap forward and discover together how to build a MySQL abstraction layer, in this case using the bridge pattern. Let’s get going!

{mospagebreak title=Creating the first abstraction layer for MySQL}

As I explained in the introduction, my intention here is to develop a simple abstraction layer by using only a bridge class and a couple of “bridged” objects. The purpose of doing this is basically to have access to the MySQL server by utilizing either the conventional “mysql” extension, or the highly-improved “mysqli” library that comes bundled with PHP 5.

Having explained how I plan to use the bridge design pattern in this specific case, let me show you the first class involved in this example. It consists of a simple wrapper for MySQL, and logically uses the respective “mysql” extension as well. Here is the signature for this brand new class. Please have a look at it:

As you can see, there’s nothing special with reference to the MySQL wrapping classes listed above. Similar to many abstraction classes that you have possibly used in many web applications, these allow you to perform some simple operations, such as connecting to the server, selecting a specific database, running queries, and processing result sets in a few useful ways.

However, the pair of MySQL wrapping classes that you just saw definitely aren’t the main topic of this article. I defined them here only with the purpose of creating another application that lets you access the MySQL server, either with the traditional “mysql” extension, or the improved “mysqli” library.

In this case, the first requirement has been already satisfied, since the previous classes use the conventional extension. Therefore, it’s time to move on and see how to develop a bridge class which will allow us to use MySQL by utilizing the two mentioned libraries.

Things are getting really exciting now! Therefore, if you want to learn how this bridge class will be created, please keep reading.

{mospagebreak title=Creating a bridge class with PHP 5}

Building a bridge class that allows you to connect to MySQL by using either the traditional “mysql” library or the “mysqli” extension is a straightforward task that can be accomplished with minor effort.

In the prior section I defined a pair of MySQL wrapping classes aimed at accessing MySQL via a conventional approach. Therefor, the process for creating a bridge class is reduced to coding the mentioned class in such a way that it can be instructed programmatically of the type of interface to be used for accessing the database server.

Does this sound rather confusing to you? It really isn’t, particularly if you take a look at the signature that corresponds to the class below:

As you can see, the “BridgeMySQLConnector” class coded above accepts the typical set of parameters required for connecting to MySQL, that is the hostname, the user/password combination and the selected database, as well as the interface to be used for performing this process.

In addition, you should realize that the previous class strictly follows the model dictated by the bridge pattern. It will use the library passed in as a parameter for accessing the database server, but in all cases there’s no specific implementation concerning how this task must be carried out.

Thus, what does all this mean? Yes, you guessed right! The logic required for connecting to MySQL has been completely decoupled from the class itself, since this procedure is tackled by the corresponding “bridged” objects, in this case referenced inside the class as “MySQL” and “mysqli” respectively. Building a bridge class with PHP 5 is actually easy after all, isn’t it?

Well, now that you know how the prior “BridgeMySQLConnector” class works, it would be very instructive to develop a practical example where all the previous classes are used in conjunction. In this way, you’ll see more clearly how this abstraction layer can be utilized for accessing the MySQL server.

As usual, if you want to learn how the layer in question will be put to work, go ahead and read the following section. I’ll be there, waiting for you.

{mospagebreak title=Putting the MySQL abstraction layer to work}

As I stated in the section that you just read, the best way to understand how this MySQL abstraction layer can be used consists of developing an illustrative example that shows the real capacity offered by the bridge pattern.

In response to this, below I coded a pair of short scripts. They first connect to MySQL using the respective “MySQL” class that you saw before, and then use the “mysqli” library for accessing the database server. Here are the corresponding code samples:

See how easy it was to connect to MySQL using either the wrapping class or the “mysqli” extension? I bet you do! In both cases, I also executed a “SELECT” statement against a hypothetical “USERS” database table, but logically this condition can be easily modified to run other SQL commands.

As you saw, learning how to apply the bridge design pattern with PHP was indeed an enjoyable experience, right?

Final thoughts

At this point, it’s quite probable that you have learned the fundamentals of using the bridge design pattern with PHP 5. Personally, I consider this pattern to be very useful for decoupling a class from its implementation, if for some reason abstract classes are not a viable approach.

Besides, its versatility allows you to apply it in a great variety of cases. Therefore I recommend that you keep it at hand if you want to achieve a high level of abstraction with your PHP-based applications. See you in the next PHP tutorial!