As I said in the section that you just read, the only thing left to explain here is how the properties of the user object created previously can be restored by a different PHP file. So, assuming that the definitions of the classes defined earlier have been included in this file, it would look similar to this: $db = new MySQL('host', 'user', 'password', 'database'); $user = new User($db); // display values of properties echo $user->name; echo $user->email; That was simple to accomplish, wasn't it? But I'd like to take this example even further. Say that it's necessary to assign new values to the "name" and "email" properties of the user object. Well, this process would be performed in the following way: $db = new MySQL('host', 'user', 'password', 'database'); $user = new User($db); // create user properties and assign values to them $user->name = 'Mary'; $user->email = 'mary@mydomain.com'; // __destruct() saves automatically object's properties to the database There you have it. Finally, I managed to build a basic class whose persistent storage mechanism is a simple MySQL table. Also, in this particular example only the properties of an instance of that class are saved to the target table, so if an application needs to save the whole instance, it'd be necessary to implement some methods that serialize/unserialize the object alternately. But, guess what? This will be left as homework for you. So get your hands dirty and get started building your own persistent objects. The experience will be really educational, trust me! Final thoughts We've finally come to the end of this series. But hopefully the whole experience has been instructive and also fun, since you learned a few different approaches to building persistent objects in PHP 5. Ranging from using cookies and plain text files to utilizing MySQL tables as the storage mechanisms associated with a particular class, creating this type of object is indeed a no-brainer process that you'll surely master in a very short time. See you in the next PHP development tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|