Before I proceed to explain how to build a sample class that can save its properties to a simple MySQL database table, first I’d like to reintroduce the example developed in the previous installment of this series. It was aimed at demonstrating how to create a similar class whose corresponding properties were stored in a specified text file. So here’s the complete definition of the aforementioned sample class. Pay close attention to it, please: class User { private $data = array(); private $file = 'data.txt';
// constructor public function __construct($file = '') { if ($file != '') { $this->file = $file; } 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); } } Aside from implementing the “__set()” and “__get()” PHP 5 magic methods, which allow you to overload easily any number of undeclared properties, the two true workhorses of the previous “User” class are its constructor and destructor methods. The first one takes care of retrieving the values assigned to those properties, while the second one saves them to a determined text file, which has been passed previously as an incoming argument. Without a doubt, the definition of this example class not only should provide you with the right pointers to start creating your own persistent classes, but it should give you a clear idea of the actual power in using magic methods to perform numerous tasks behind an extremely simple API. Now that you've surely grasped how this sample class does its thing, below I included a script that shows how to use an instance of it in a concrete case. Study the following code fragment: // example on creating a persistent object $user = new User('newfile.txt'); $user->name = 'Alejandro'; $user->email = 'alejandro@mydomain.com'; // __destruct() saves automatically the object to the target file As I said before, thanks to the flexible nature of the “User” class, it’s extremely simple to spawn a user object, dynamically create a couple of properties, and then save them to a specific text file -- in this case named “newfile.txt.” Naturally, the object’s destructor is responsible for storing the properties in the target file, right before the PHP interpreter finishes executing the script. On the other hand, it’s possible to restore those properties or assign new values to them in a different file, provided that the definition of the “User” class has been included previously. This last task is performed by the following script: // assign new properties to the persistent object $user = new User('newfile.txt'); $user->name = 'Mary'; $user->email = 'mary@domain.com'; // __destruct() saves automatically the object to the target file Simple to code and read, isn’t it? As you can see, the properties of the user object created in a previous file are assigned a pair of brand new values, which are again saved to the target text file, thus showing how they can persist across different HTTP requests. So far, everything looks good; you've learned how to create persistent classes that maintain their state by means of a plain text file. So, what’s the next step to take? Well, as I explained at the beginning of this tutorial, in most cases the storage mechanism used by persistent classes in PHP 5 is simply a MySQL table. Therefore, in the following section I’m going to build a basic MySQL abstraction class, which will be utilized by a persistent object to save its properties for further manipulation. Now, if you wish to see how this brand new MySQL-accessing class will be defined, click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|