PHP
  Home arrow PHP arrow Page 3 - An Object-based Approach to HTTP Compression in PHP
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? 
Google.com  
PHP

An Object-based Approach to HTTP Compression in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2006-04-17


    Table of Contents:
  • An Object-based Approach to HTTP Compression in PHP
  • Object-based "Gzip" compression: creating a data compressor PHP class
  • Setting up a concrete example: putting the "DataCompressor" class to work
  • Explaining the example

  • 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


    An Object-based Approach to HTTP Compression in PHP - Setting up a concrete example: putting the "DataCompressor" class to work
    ( Page 3 of 4 )

    In order to demonstrate in a friendly way how the "DataCompressor" class can be used within an object-oriented development environment, I'll use the same "sample_file.php" file that you saw in my previous tutorial. As you'll recall, it displayed some rows from a "users" database table. In addition, I'll list the two MySQL processing classes used by this sample file. The example begins by showing both MySQL-related classes:

    // define 'MySQL' class
    class MySQL{
        private $host;
        private $user;
        private $password;
        private $database;
        private $connId;
        // constructor
        function __construct($options=array()){
            if(!is_array($options)){
                throw new Exception('Connection options must be an
    array');
            }
            foreach($options as $option=>$value){
                if(empty($option)){
                    throw new Exception('Connection parameter cannot
    be empty');
                }
                $this->{$option}=$value;
            }
            $this->connectDb();
        }
        // private 'connectDb()' method
        private function connectDb(){
            if(!$this->connId=mysql_connect($this->host,$this-
    >user,$this->password))
    {
                throw new Exception('Error connecting to MySQL');
            }
            if(!mysql_select_db($this->database,$this->connId)){
                throw new Exception('Error selecting database');
            }
        }
        // public 'query()' method
        public function query($sql){
            if(!$result=mysql_query($sql)){
                throw new Exception('Error running query '.$sql.'
    '.mysql_error());
            }
            return new Result($this,$result);
        }
    }
    // define 'Result' class
    class Result{
        private $mysql;
        private $result;
        // constructor
        public function __construct($mysql,$result){
            $this->mysql=$mysql;
            $this->result=$result;
        }
        // public 'fetch()' method
        public function fetch(){
            return mysql_fetch_array($this->result,MYSQL_ASSOC);
        }
        // public 'count()' method
        public function count(){
            if(!$rows=mysql_num_rows($this->result)){
                throw new Exception('Error counting rows');
            }
            return $rows;
        }
        // public 'get_insertId()' method
        public function getInsertId(){
            if(!$insId=mysql_insert_id($this->mysql->connId)){
                throw new Exception('Error getting insert ID');
            }
            return $insId;
        }
        // public 'seek()' method
        public function seek($row){
            if(!int($row)&&$row<0){
                throw new Exception('Invalid row parameter');
            }
            if(!$row=mysql_data_seek($this->mysql->connId,$row)){
                throw new Exception('Error seeking row');
            }
            return $row;
        }
        // public 'getAffectedRows()' method
        public function getAffectedRows(){
            if(!$rows=mysql_affected_rows($this->mysql->connId)){
                throw new Exception('Error counting affected rows');
            }
            return $rows;
        }
        // public 'getQueryResource()' method
        public function getQueryResource(){
            return $this->result;
        }
    }

    Now that you've hopefully recalled how the above MySQL processing classes looked, it's time to list the corresponding "sample_file.php" file. Here's the respective definition for this file:

    try{
        // include class files
        require_once 'mysqlclass.php';
        require_once 'resultclass.php';
        // connect to MySQL
        $db=new MySQL(array
    ('host'=>'host','user'=>'user','password'=>'password',
    'database'=>'database'));
        // run SQL query
        $result=$db->query('SELECT * FROM users');
        // display results
        while($row=$result->fetch()){
            echo $row['id'].$row['name'].$row['email'].'<br />';
        }
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    As the above example clearly shows, the "sample_file.php" file first includes the corresponding MySQL processing classes, in order to connect to the MySQL server, and then performs a simple SELECT statement to fetch a few rows from a sample "users" database table. Once the database rows have been returned by the query, they're simply displayed on the browser, as one expects when proceeding with a regular MySQL result set.

    Of course, since I'm currently using PHP 5 as my testing platform, the whole code is wrapped by a single "try-catch" block, in order to trap all the potential exceptions that could eventually be triggered by the different sections of the script. As you can see, all the tasks performed by the "sample_file.php" file are very understandable.

    Now that you clearly understand how the previous sample file works, take a look at the following piece of code, which demonstrates a practical implementation for the "DataCompressor" class:

    try{
        // instantiate a new 'DataCompressor' object
        // pass the 'sample_file.php' file in order to compressing
    its dynamic output
        $dataComp=new DataCompressor('sample_file.php');
        // send Gzip http header
        $dataComp->sendEncodingHeader();
        // uncompress & display data
        echo $dataComp->fetchCompressedData();
    }
    // catch all possible exceptions
    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    If you study the example shown above, then you'll definitely agree with me that using the "DataCompressor" class is really a simple and straightforward process.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek