Since the “Cache” class implements the necessary logic to manipulate data either from a query result or from the cache file, I’ll explain in detail the changes applied to its structure. As you hopefully remember, the class utilizes an array structure for handling result sets, which are serialized for caching purposes and unserialized for post processing. Considering that the majority of the class operations are carried out on simple arrays, it’d be highly desirable to develop an array iterator class that handles the most common tasks associated with arrays, and then create an instance of it (again, composition plays an important role) within the “Cache” class. Bearing this concept in mind, let’s have a look at the updated definition for the “Cache” class. It is as follows: class Cache{ As you can see, now the class includes three objects for doing its work: a reference of a MySQL object, $this->mysql (this object is aggregated), an instance of a “Result” object, that is $this->result, and finally an instance of the array processor $this->arrproc, for performing array operations. If this sounds rather confusing, let’s explain the logic of some methods. For instance, the “fetchRow()” method now uses the “getCurrentElement()” method that belongs to the array processor for fetching the current element within the result set array. Similarly, this technique is used to count rows, through the “countRows()” method, or even fetch a limited result set using “fetchRange()”. See how we’re using the functionality provided by the array processor? Now, the inclusion of this object should be clear for the correct implementation for the “Cache” class. By assigning clearly delineated responsibilities to different classes, we’re writing better and more portable code that can be reused in any number of projects. Of course, there are many things to be reviewed yet, in order to get the big picture about the caching system. Certainly, we need to see in detail the code for the array processor class, as well as how the classes fit into the application. All right, don’t get anxious, because these topics will be fully explained in the next part of the series. To wrap upThrough the course of this part, we’ve seen in detail the updated version of each class that composes the caching system, for a correct implementation in PHP 5. Also, the “Cache” class has been modified to work with an array processor class that handles array operations. Hopefully, the article has been didactic and useful for refreshing techniques related to object-oriented programming. With all of the code exposed here, I guess you’ll spend a while adapting it to suit your needs. However, don’t miss the next part, where the complete caching system is put to work. Until then, stay in touch!
blog comments powered by Disqus |