In the course of the previous segment, I partially defined a sample class, which had the ability to create and assign properties dynamically. It had this ability due to a simple implementation of the so-called property overloading process, carried out by the complementary “__set()” and “__get()” PHP magic functions. However, the most important point to note with reference to this class is that I intend to make it capable of saving an instance of itself to a session variable. So far, its factory method does load this instance, but there’s no a specific method that first stores the object on the session variable. So how can this be done? Well, as you may know, there are many ways to save an instance of a class to a session variable. For instance, I could define a regular method called “save()” or something similar to do this, but in this particular case I’m going to use a destructor to perform this task. In doing so, the whole saving process will be handled transparently by the PHP engine. Of course, it’s important to learn how to implement a destructor within the previous “User” class, so below I included its basic definition: // save object to session variable public function __destruct() { $_SESSION['user'] = serialize($this); } Were you expecting to see lengthier code? Fortunately, this is not the case. The implementation of this destructor does only one thing, namely save an instance of the “User” class to a predefined session variable. Since a destructor method is always called automatically right before the PHP interpreter finishes parsing a script, the instance will be stored via the session mechanism in a truly transparent manner. Now that you've grasped the underlying logic that drives the previous destructor method, it’s time to see the complete definition of the “User” class, after adding this last method to it: class User { private $data = array();
// constructor (not implemented) public function __construct(){}
// factory method public static function factory() { session_start(); if(isset($_SESSION['user']) === TRUE) { return unserialize($_SESSION['user']); } return new User(); }
// set undeclared property public function __set($property, $value) { $this->data[$property] = $value; }
// get undeclared property public function __get($property) { if (isset($this->data[$property]) === TRUE) { return $this->data[$property]; } }
// save object to session variable public function __destruct() { $_SESSION['user'] = serialize($this);
} } Definitely, I’m not trying to pretend that creating persistent objects in PHP 5 is always that simple, but you’ll have to admit that understanding how the above “User” class persists during different HTTP requests is a straightforward process. Even so, a concrete example is in order here, so you can see how to work with an instance of this sample class. Thus, in the last segment of this tutorial I’m going to create a script that first assigns some properties to an object spawned from the class, and then uses its destructor to save it to the $_SESSION[‘user] variable that you saw before. Therefore, if you wish to learn how this example will be developed, click on the link displayed below and read the next section.
blog comments powered by Disqus |
|
|
|
|
|
|
|