HomePHP Using Different Target Files for Persistent Objects
Using Different Target Files for Persistent Objects
Persistent objects appear at first to be an obscure and hard-to-grasp subject that belongs under software development. In fact, persistent objects are simply regular objects spawned from a class that has some form of storage mechanism associated with it. This six-part series shows you how to get the most out of persistent objects in your web applications.
In the particular case of PHP 5, which as you know is well-suited for building web applications, persistent objects are very often created to overcome the stateless nature of the HTTP protocol. This implies defining classes that are capable of saving the state of their instances to a database table, a text file or a plain cookie. (Keep in mind, though, that this last option is considered to be a pretty unsafe solution because it relies heavily on the client’s environment to work properly).
Naturally, if you’ve been a patient reader and had the chance to read all of the previous articles in this series, then by now building persistent objects in PHP 5 should be familiar to you. In those articles I've used a decent number of code samples to help explain how to create these objects. My first examples showed how to do it with a few cookies; later, I demonstrated the technique using a predefined text file.
More specifically speaking, in the last article I defined a simple class that could save the properties assigned to its instances to a unique file called “data.txt.” While this example class could be considered a genuine persistent entity, I have to admit that in its current state, it lacks some flexibility. After all, it can only store its properties in a single text file, which is not too useful.
To address this limitation, in this fourth part of the series I’m going to tweak the definition of the class created in the previous article. The modification will allow it to save its properties to different text files, thus considerably enhancing its functionality.
Are you ready to learn more about building persistent objects with PHP 5? Then start reading now!