PHP
  Home arrow PHP arrow Page 3 - Caching Result Sets in PHP: Object Interaction Within a Caching System
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Caching Result Sets in PHP: Object Interaction Within a Caching System
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2005-10-17


    Table of Contents:
  • Caching Result Sets in PHP: Object Interaction Within a Caching System
  • The first link in the caching process: looking at the “Cache” class
  • The second link in the caching process: overview of the “MySQL” class
  • The third link in the caching process: a quick look at the “Result” class
  • Chaining the links: putting the classes together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Caching Result Sets in PHP: Object Interaction Within a Caching System - The second link in the caching process: overview of the “MySQL” class
    ( Page 3 of 5 )

    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.



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT