Home arrow PHP arrow Page 4 - A Persistent Class in Action

Retrieving properties of a persistent object on a different web page - PHP

If youíre a PHP developer who wants to learn how to create objects that can maintain their state through different HTTP requests, either by using cookies, plain text files or MySQL database tables, then donít look any further because youíve come to the right place. Welcome to the last part of a six-part series on building persistent objects in PHP 5. This tutorial series introduces the key concepts that surround the creation of persistent objects, and complements the corresponding theory with copious code samples.

TABLE OF CONTENTS:
  1. A Persistent Class in Action
  2. Review: creating a database-driven persistent class
  3. Spawning a persistent object from the User class
  4. Retrieving properties of a persistent object on a different web page
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
September 30, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: