Home arrow PHP arrow Page 3 - Caching Result Sets in PHP: Object Interaction Within a Caching System

The second link in the caching process: overview of the “MySQL” class - PHP

In this article, we work directly with a standalone caching class, showing how it interacts with other objects. We will work with aggregation and composition to achieve our goals, which include implementing a time expiry based caching mechanism.

  1. Caching Result Sets in PHP: Object Interaction Within a Caching System
  2. The first link in the caching process: looking at the “Cache” class
  3. The second link in the caching process: overview of the “MySQL” class
  4. The third link in the caching process: a quick look at the “Result” class
  5. Chaining the links: putting the classes together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
October 17, 2005

print this article



Building a MySQL wrapping class is something that has been done many times, and probably you’ve been working with existing packages or developing your own MySQL class. Keeping this idea in mind, I won’t explain how to build such an application. Instead, I’ll show a rather simple version of a regular MySQL abstraction class, but one that will be useful for aggregation by the “Cache” class.

The structure of the “MySQL” wrapping class looks like this:

class MySQL{
            var $conId; // connection identifier
            var $host; // MySQL host
            var $user; // MySQL username
            var $password; // MySQL password
            var $database; // MySQL database
            // constructor
            function MySQL($options=array()){
                        // validate incoming parameters
                                    foreach($options as
$this->{$parameter}=$value:$this->isError('Invalid parameter
                                   // connect to MySQL
                        else {
                                   $this->isError('No connection
parameters were provided');
            // connect to MYSQL server and select database
            function connectDB(){
connecting to the server');
selecting database');
            // perform query
            function query($query){
performing query '.$query);
                        // return new Result object
                        return new Result($this,$this->result);
            // display errors
            function isError($errorMsg){
                       trigger_error($errorMsg.' '.mysql_error

As you can see, the above listed class is not rocket science. It presents the classical methods for connecting to MySQL, selecting a database, running queries and so forth. Also, I’ve opted to build a method that handles potential errors in a generic way, which provides a centralized point for managing errors within the client code.

However, as I’ve mentioned before, it’s much better having a separate class that carries out error handling, whether you’re working with PHP 4 or PHP 5 (where exceptions are well supported). Unfortunately, error handling is out of the scope of this series, so, for the moment, we’ll settle for having an “isError()” method.

All right, since the class is very understandable, it should be clear how it’s aggregated by the “Cache” class. Here, we’re using the power of aggregation to make the caching class work properly.

But, I mentioned that composition would play a relevant role in the developing process. So, where does composition take place? Well, if we take a look at the “query()” method, we see that an instance of a “Result” object is created. Certainly, this new object composes the “MySQL” object.

What’s more, we’re using the Factory Pattern at a very basic level, since the instantiation of a “Result” object is rather decoupled from the client code. Now, all of the classes nicely fit each other.

Having a “Result” object dynamically created directly implies the existence of the corresponding class. Thus, let’s jump into the next section to see the definition for the “Result” class.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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