HomePHP 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.
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 if(count($options)>0){ foreach($options as $parameter=>$value){ (!empty($value))? $this->{$parameter}=$value:$this->isError('Invalid parameter '.$parameter); } // connect to MySQL $this->connectDB(); } else { $this->isError('No connection parameters were provided'); } } // connect to MYSQL server and select database function connectDB(){ if(!$this->conId=mysql_connect($this- >host,$this->user,$this->password)){ $this->isError('Error connecting to the server'); } if(!mysql_select_db($this- >database,$this->conId)){ $this->isError('Error selecting database'); } } // perform query function query($query){ if(!$this->result=mysql_query ($query,$this->conId)){ $this->isError('Error performing query '.$query); } // return new Result object return new Result($this,$this->result); } // display errors function isError($errorMsg){ trigger_error($errorMsg.' '.mysql_error ()); exit(); } }
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.