To demonstrate how an instance of the enhanced “User” class can persist across two HTTP requests, I’m going to define a couple of PHP files. The first one will create a new user object, and the second will assign new values to its properties, once the object has been reloaded via its factory method. Does this sound rather confusing to you? Fear not; just look at the definition corresponding to the first of these files: <?php
class User { private $data = array(); private static $key = 'user';
// constructor (not implemented) public function __construct(){}
// factory method public static function factory($key = '') { session_start(); if ($key != '') { self::$key = $key; } if(isset($_SESSION[self::$key]) === TRUE) { return unserialize($_SESSION[self::$key]); } 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[self::$key] = serialize($this); } }
// example on creating a persistent object $user = User::factory('newuser'); $user->fname = 'Alejandro'; $user->lname = 'Gervasio'; $user->email = 'alejandro@mydomain.com';
?> Frankly speaking, nothing spectacular is happening here. The previous file shows how to create a brand new user object, which, thanks to the enhanced functionality of its originating class, is now stored on a different session variable called $_SESSION['newuser']. On the other hand, it’s necessary to create a second file that retrieves this object and modifies its properties as well. The one below does that in a simple manner: <?php
class User { private $data = array(); private static $key = 'user';
// constructor (not implemented) public function __construct(){}
// factory method public static function factory($key = '') { session_start(); if ($key != '') { self::$key = $key; }
if(isset($_SESSION[self::$key]) === TRUE) { return unserialize($_SESSION[self::$key]); } 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[self::$key] = serialize($this); } }
// assign new values to properties of the persistent object $user = User::factory('newuser'); $user->fname = 'John'; $user->lname = 'Doe'; $user->email = 'john@domain.com';
?> As I expressed earlier, this second file simply reloads the user object created earlier via its factory method, and then assigns some new values to the corresponding properties. Finally, the new state of the object is saved internally to the specified session variable by means of its destructor, which demonstrates in a nutshell how easy it is to extend the functionality of the persistent “User” class. And with this last example I’m finishing this article. As usual, feel free to edit all of the code samples shown in this tutorial, which hopefully will give you a more solid background in building persistent objects with PHP 5. Final thoughts That’s all for now. In this second part of the series, I demonstrated how to build a persistent class that has the ability to save its instances to different session variables. As you saw for yourself, building this particular class was pretty straightforward, so in theory you shouldn’t have major problems replicating it on your own testing web server. Moving forward, in the next tutorial I’m going to discuss how to create persistent objects that use text files as their default storage mechanism, unlike simple cookies. So, now that you know what to expect from the forthcoming part of this series, you can’t miss it!
blog comments powered by Disqus |
|
|
|
|
|
|
|