PHP
  Home arrow PHP arrow Page 3 - Caching Result Sets in PHP: Object Int...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 4 stars4 stars4 stars4 stars4 stars / 4
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · In this fourth part of the series, the result-set caching class is integrated with...
       · Great set of articles, it's exactly what I was looking for.I am having some...
       · Hello,Thanks for commenting on the article. Regarding your question, the reason...
       · I'm just wondering why not using PEAR :: DB instead of creating new classes for...
       · Thanks for the comments on my article and my classes.In fact, I'm not reinventing...
       · Thank you for answering back.I do see your point, but let me add this. It's true...
       · Thank you for your reply.I understand your point too. What you said about PEAR...
       · I have coded in several different languages prior to using PHP, and have more of a ...
       · I'm glad to know that this series have been quite helpul for you in learning the OOP...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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