Home arrow PHP arrow Page 3 - An Object-based Approach to HTTP Compression in PHP

Setting up a concrete example: putting the "DataCompressor" class to work - PHP

Welcome to the second tutorial of the series “Using HTTP compression in PHP.” In three parts, this series demonstrates how to apply HTTP compression to parsed PHP pages, in order to reduce their overall download times, which results in faster delivery of dynamic content.

  1. An Object-based Approach to HTTP Compression in PHP
  2. Object-based "Gzip" compression: creating a data compressor PHP class
  3. Setting up a concrete example: putting the "DataCompressor" class to work
  4. Explaining the example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
April 17, 2006

print this article



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()){
            throw new Exception('Connection options must be an
        foreach($options as $option=>$value){
                throw new Exception('Connection parameter cannot
be empty');
    // private 'connectDb()' method
    private function connectDb(){
            throw new Exception('Error connecting to MySQL');
            throw new Exception('Error selecting database');
    // public 'query()' method
    public function query($sql){
            throw new Exception('Error running query '.$sql.'
        return new Result($this,$result);
// define 'Result' class
class Result{
    private $mysql;
    private $result;
    // constructor
    public function __construct($mysql,$result){
    // public 'fetch()' method
    public function fetch(){
        return mysql_fetch_array($this->result,MYSQL_ASSOC);
    // public 'count()' method
    public function count(){
            throw new Exception('Error counting rows');
        return $rows;
    // public 'get_insertId()' method
    public function getInsertId(){
            throw new Exception('Error getting insert ID');
        return $insId;
    // public 'seek()' method
    public function seek($row){
            throw new Exception('Invalid row parameter');
            throw new Exception('Error seeking row');
        return $row;
    // public 'getAffectedRows()' method
    public function getAffectedRows(){
            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:

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

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:

    // 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
    // uncompress & display data
    echo $dataComp->fetchCompressedData();
// catch all possible exceptions
catch(Exception $e){
    echo $e->getMessage();

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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