Before I begin demonstrating how to make the persistent class defined in the previous article slightly more flexible, first I’d like to summarize how to use it in its current state. This way, you'll see how an instance of it can be saved to a specific predefined variable, and at later time, restored on a different web page. So here is the complete definition of the persistent class. I decided to call it “User,” since it's useful for storing data on a fictional user. Have a look at 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); } } Well, to be frank, understanding how an instance of the previous “User” class can persist across different HTTP requests isn’t rocket science. As you can see, the class restores (or creates) a user object via its “factory()” method, while its destructor takes care of storing the corresponding instance to a statically-assigned session variable, called “$_SESSION['user']. It’s really that simple. Now that you remember how the previous “User” class was defined originally, it’s time to demonstrate its actual functionality. To do that, I’m going to spawn an object from it, which will store some data about me on the specified session variable (and don’t think that I’m being too egocentric, since it’s only for example purposes). The code fragment that performs this task is the following: // example on creating a persistent object $user = User::factory(); $user->fname = 'Alejandro'; $user->lname = 'Gervasio'; $user->email = 'alejandro@mydomain.com'; Behind this short script, a few interesting things are happening. First, it creates a new user object by way of the static “factory()” method, and then it uses property overloading to assign dynamically some values to three undeclared properties. Finally, it saves the object to the $_SESSION['user'] variable via the class’s destructor. Of course, it’s also possible to create persistent objects in a more conventional way, without using some of the magic methods offered by PHP 5, but I decided to code the “User” class like this to make it a bit more compact. Now, returning to the previous code sample, once the user object has been saved to the specified session variable, it’s necessary to show how it can be restored and modified on a different web page. To achieve this, the page in question should look similar to this: <?php
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);
} }
// assign new properties to the persistent object $user = User::factory(); $user->fname = 'John'; $user->lname = 'Doe'; $user->email = 'john@domain.com';
?> From the above example, it’s clear to see how easy it is to retrieve the persistent object created previously on a different web page, as well as to assign new values to its properties. In this particular case, I decided to modify those properties, but naturally it’s possible to load the object via its “factory()” method and keep its internal state untouched. That was really simple to do, wasn’t it? So far, so good. At this stage, I showed you how to create and use an object that’s capable of maintaining its status through two different web pages by using only a single session variable. However, as I said in the introduction, the object’s originating class in its current state is pretty inflexible, since it stores the pertinent instance on a predefined variable that can’t be inputted from the outside. In reality, it’d be useful to provide the class with the ability to store its instances on different session variables, other than the predetermined $_SESSION['user'] that you saw before. Therefore, in the section to come I’m going to modify slightly the definition of the sample “User” class, so it can expose the aforementioned capability. Now, read the following segment. It’s only one click away.
blog comments powered by Disqus |
|
|
|
|
|
|
|