If you still haven’t read the previous installment of this series, where I explained how to build a basic class that could save its properties to a predefined text file, don't worry. Below I've included this class’s complete definition, along with a short script that demonstrates how to use it. Here’s the full source code of this persistent class, so pay attention to it: class User { private $data = array(); private $file = 'data.txt';
// constructor public function __construct() { list($this->data['name'], $this->data['email']) = explode('|', file_get_contents($this->file)); }
// set undeclared property public function __set($property, $value) { if ($property !== 'name' and $property !== 'email') { return; } $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() { file_put_contents($this->file, $this->name . '|' . $this->email); } } Definitely, the above code fragment demonstrates how simple it is to build a class that can save the properties of its instances to a specific file, in this particular case called “data.txt.” Of course, the implementation of its destructor makes the whole saving process really simple. It's performed transparently when the PHP interpreter invokes this method before it finishes executing the calling script. And now that you grasped the logic driving the previous class, here’s a script that populates the properties of a user object with some trivial values, and then stores them in the specified text file: // example on using a persistent object with a text file $user = new User(); $user->name = 'Mary'; $user->email = 'mary@mydomain.com'; // __destruct() saves automatically the object to the target file Once the properties have been properly saved to the “data.txt” file, it’s possible to retrieve them on a different web page, as shown below, if the definition of the class has been previously included. Take a look: // assign new values to properties of the persistent object $user = new User(); $user->name = 'Susan'; $user->email = 'susan@domain.com'; // __destruct() saves automatically the object to the target file There you have it. As you can see, creating persistent objects in PHP 5 is much easier than you might think, right? However, in keeping with the concepts described in the introduction, it’s feasible to make the example “User” class a bit more flexible by providing it with the ability to save its instances to a text file other than the default “data.txt.” But how can this be done in a few easy steps? Well, the upgrade process would be as simple as refactoring the class’s constructor. The constructor needs to be able to take, as an input argument, the name of the file that will be used for storing the corresponding properties. The full details of this process will be discussed in detail in the section to come. To learn more, read the following segment.
blog comments powered by Disqus |
|
|
|
|
|
|
|