HomePHP Saving Class Instances to Files with Persistent Objects
Saving Class Instances to Files with Persistent Objects
Welcome to the third part of a six-part series on building persistent objects in PHP 5. Through a decent variety of code samples, this series provides you with the right pointers to start creating objects that can save themselves to a persistent storage mechanism, including simple cookies, plain text files, and MySQL database tables.
Though it may sound scary and a bit intimidating, creating objects in PHP 5 that can maintain their state through a number of HTTP requests is an extremely simple process. It only requires an intermediate background in the object-oriented paradigm, along with a basic understanding of how to serialize/unserialize objects.
So, if you’re a PHP developer who wants to master the key concepts that surround the creation of this kind of object without having to spend endless hours reading the PHP manual, then this group of articles may be the material that you’re looking for.
And now that you've been introduced to the main goal of the series, it’s time to review the topics that were covered in the last article. In that tutorial I explained how to build a basic PHP 5 class which was capable of saving an instance of itself to a specified session variable. This ability allowed it to persist across different web pages.
Also, it’s worth mentioning that this class implemented a static factory method, which allowed it to retrieve class instances in a truly painless way, without our having to explicitly code any constructor.
Though, as I said a few moments ago, a persistent object can also use different mechanisms to maintain its status; that naturally includes text files. Thus, in this third chapter of the series, I’m going to discuss how to build a class that stores instances of itself in a specified file.
Are you ready to learn more on this topic? Start reading right now!