Before I start explaining how to build a class capable of saving its instances to a text file, I'm going to reintroduce the example developed in the preceding tutorial. It demonstrated how to create a similar class, but in that particular case, it used a session variable as its default storage mechanism. First, here’s the complete source code corresponding to that persistent class, so you can quickly recall how it originally looked: 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); } } As I explained before, the above “User” class can save an instance of itself to a custom session variable, in this case by giving a basic implementation to its destructor. On the other hand, its factory method permits you to load or create a user object, and eventually to assign new values to its properties. These two well-differentiated processes are shown by the following script: // example on creating a persistent object $user = User::factory('newuser'); $user->fname = 'Alejandro'; $user->lname = 'Gervasio'; $user->email = 'alejandro@mydomain.com'; I believe that this code fragment should be easy for you to grasp. It first spawns a brand new user object, and then it creates dynamically some properties along with their corresponding values. Finally, it saves the object to a session variable, which for this occasion happens to be referenced as $_SESSION['newuser']. Once the previous user object has been stored in the session variable, say that it needs to be retrieved on a different web page for further manipulation. In a case like this, the code snippet below will perform that task in a simple manner: // assign new values to properties of the persistent object $user = User::factory('newuser'); $user->fname = 'Mary'; $user->lname = 'Smith'; $user->email = 'mary@domain.com'; What’s more, if incidentally the previous user object is required by another file, then the whole retrieval process would be as simple as loading it via its factory method, as shown below: $user = User::factory('newuser'); So far, so good. At this point, I’m sure that you already grasped the underlying logic that drives the previous example, meaning that it’s time to explore a few other ways to create persistent objects with PHP 5. So, as I stated in the introduction, it’s possible to build this type of object by using a plain text file as the default storage mechanism instead of working with cookies, which as you know can be disabled in the client. Provided that you’re interesting in learning how to accomplish this, in the following section I’m going to define a whole new persistent class, which obviously will be capable of saving instances of itself to a specified text file. To see how this class will be built, click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|