Before I make a data compressor class that internally uses "Gzip" encoding for compressing the data passed as input argument, first let me list the "getCompressedContent()" function that I wrote in the previous tutorial, to refresh your memory of how this function looked. Here is its signature: function getCompressedContent($data){ Now, I hope the above function is pretty familiar to you, because I'm going to use it as the foundation for building the data compressor class that I mentioned before. Particularly, I'll define this class for use in PHP 5, but you can easily adapt its source code to include it within your PHP 4 scripts. Here's how the "DataCompressor" class looks: // define 'DataCompressor' class As I said earlier, I wrote the above class inspired by the source code of the previous "getCompressedContent()" function, something that should be quite obvious. As you can see, the workhorse of the class is the "fetchCompressedData()" method, which is very similar to its procedural counterpart. In this case, this method opens up an output buffer, then includes the contents of the $this->data property, and finally returns the "gzip-encoded" data, after cleaning up the respective buffer. Additionally, the class constructor accepts the mentioned $data incoming argument, which is properly checked inside this method to make sure it's either a file or a simple string. Any other type of data passed on to the constructor will throw an exception, which eventually will be caught by client code. Finally, I decided to define separately the "sendEncodingHeader()" method, providing the class with the ability to send this HTTP header when the method is specifically called. Definitely, breaking down the class code into different specific methods helps keep the whole class as a much more flexible programming structure. Right, now that I've shown you the signature of the "DataCompressor" class, you'll probably want to see how it can be used within a basic sample PHP script. For this reason, jump into the next section, so you can learn how this class is utilized as part of a pretty useful hands-on example.
blog comments powered by Disqus |