HomePHP Page 4 - An Object-based Approach to HTTP Compression in PHP
Explaining the example - 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.
First, the script instantiates a new "DataCompressor" object, and secondly passes the sample PHP file that you just saw to the constructor. Since the source code of the "fetchCompressedData()" method will include this file after opening an output buffer, its whole dynamic output will be first "crunched" (that is, all new line characters will be removed) and finally compressed by the "Gzip" encoding algorithm.
Before the data is actually encoded, the script calls the "sendEncodingHeader()" method, in this way indicating to the browser that contents will be transmitted compressed over the network. At the end of the script execution, the data is transparently decoded by the browser, and finally displayed to the user.
As you can see, the whole data compression/decompression process is handled by the "DataCompressor" class in combination with the corresponding user agent, which means that using HTTP compression to make dynamic PHP pages load faster is quite easy to implement in several PHP scripts.
To wrap up
Unfortunately, that's all for the moment. Throughout this second part of the series, I demonstrated how to use HTTP compression in object-based development environments, by creating a data compressor class in PHP 5. As you learned in the article, this class utilizes a few simple methods for fetching incoming data, and then returns the contents properly compressed. Additionally, it allows you to send the corresponding HTTP header using a separate method, which makes the class even more flexible. Thus, if you feel inclined to extend its functionality, go ahead and develop a full-featured HTTP compression system.
In the last article of the series, I'll explain how to take advantage of HTTP compression within a PHP web page generator system, which uses objects to create dynamic web documents. Curious about how this will be achieved? You'll have to wait till the next part!