Home arrow PHP arrow Page 4 - Caching Result Sets in PHP: Porting the Code to PHP 5

Yet another modified class: updating the “Result” class - PHP

In this part of the series, you will see in detail an updated version of each class that composes the caching system, for a correct implementation in PHP 5. Also, the “Cache” class will be modified to work with an array processor class that handles array operations. It should help refresh your memory of techniques related to object-oriented programming.

TABLE OF CONTENTS:
  1. Caching Result Sets in PHP: Porting the Code to PHP 5
  2. One step prior to migrating: caching result sets with PHP 4
  3. Taking advantage of an improved object model: updating the caching system to PHP 5
  4. Yet another modified class: updating the “Result” class
  5. The caching system’s core class: updating the “Cache” class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
October 31, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As you may probably guess, the process for updating the “Result” class is pretty straightforward. Applying the same modifications introduced to the MySQL class, we end up defining the code for this class as follows:

class Result {
            private $mysql; // instance of MySQL object
            private $result; // result set
            // constructor
            public function __construct(&$mysql,$result){
                        $this->mysql=&$mysql;
                        $this->result=$result;
            }
            // fetch row
            public function fetchRow(){
                        return mysql_fetch_array($this-
>result,MYSQL_ASSOC);
            }
            // count rows
            public function countRows(){
                        if(!$rows=mysql_num_rows($this->result)){
                                   throw new Exception('Error
counting rows');
                        }
                        return $rows;
            }
            // count affected rows
            public function countAffectedRows(){
                        if(!$rows=mysql_affected_rows($this-
>mysql->conId)){
                                   throw new Exception('Error counting affected rows');
                        }
                        return $rows;
            }
            // get ID from last inserted row
            public function getInsertID(){
                        if(!$id=mysql_insert_id($this->mysql-
>conId)){
                                   throw new Exception('Error
getting ID');
                        }
                        return $id;
            }
            // seek row
            public function seekRow($row=0){
                        if(!mysql_data_seek($this->result,$row)){
                                   throw new Exception('Error
seeking data');
                        }
            }
}

Once again, I’ve explicitly defined the class’ member visibility, as well as delegated runtime error conditions by throwing exceptions within conflicting sections of the code. That way, either when fetching result rows or moving the result set’s internal pointer, potential errors are easily handled by an exception mechanism.

With the two above classes already updated to PHP 5, it’s time to look at the workhorse of the whole caching system: the “Cache” class. Let’s jump to the next section to find out how this class is properly updated.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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