HomePHP 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.
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.