HomePHP Persistent Objects and Different Session Variables in PHP 5
Persistent Objects and Different Session Variables in PHP 5
Despite its rather intimidating name, persistent objects represent a pretty simple concept applied very often in software development. It's aimed at creating structured entities that can maintain their status across different stages of an application. This six-part series will take the mystery out of persistent objects so you can use them in your own applications.
When using PHP 5 for developing web-based programs, creating this kind of object is even simpler. In most cases, the creation process is reduced to defining classes that are simply capable of keeping their state through several HTTP requests, in order to overcome the stateless nature of this protocol.
Naturally, popular storage mechanisms long available on the web like database tables, text files and even cookies are frequently used to save instances of a class. This implies that some objects may be more persistent that others, even when this characteristic is only relevant in the context in which they’re utilized.
However, as with many other topics related specifically to web development, building persistent objects in PHP 5 is a subject that’s definitely much better understood by example. So, in the first part of this series of articles I demonstrated how to define a basic class, which was provided with the ability to save an instance of itself to a predefined session variable, in this way maintaining its state across several web pages.
There are, however, a few issues that must be fixed regarding the way that this persistent class was initially defined. First, it’s necessary to show how to restore an instance of the class on a different web page, and second, it must have the ability to use different session variables for saving its instances, thus enhancing its flexibility.
Therefore, in this second installment of the series I’m going to show how to address the aforementioned issues. Let’s get started right now!