Home arrow PHP arrow Page 4 - Building Persistent Objects in PHP 5

Putting the sample User class into action - PHP

The HTTP protocol is stateless, but sometimes it is necessary to make web applications store or remember information. This is sometimes referred to as persistent storage, and it takes on a number of different forms. This six-part series of articles will explain the concept and show you various ways to give your web applications a memory.

  1. Building Persistent Objects in PHP 5
  2. Introducing persistent objects in PHP 5
  3. Implementing a destructor method
  4. Putting the sample User class into action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
September 15, 2009

print this article



As I said in the segment that you just read, the best way to understand how an instance of the ďUserĒ class defined earlier can be turned into a persistent object is by means of a simple example.

Below I included the complete source code of the class, complemented by a short script that shows how to create an object that persists across multiple HTTP requests, naturally assuming that session support has been enabled on the testing web server.

Hereís the corresponding code sample, so take your time to examine it closely:

class User


private $data = array();


// constructor (not implemented)

public function __construct(){}


// factory method

public static function factory()



if(isset($_SESSION['user']) === TRUE)


return unserialize($_SESSION['user']);


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['user'] = serialize($this);





// example on using a persistent object

$user = User::factory();

$user->fname = 'Alejandro';

$user->lname = 'Gervasio';

$user->email = 'alejandro@mydomain.com';

There you have it. Believe it or not, creating a basic persistent object with PHP 5 is really a simple process, as demonstrated by the above code fragment. In this case, the ď$userĒ object, which happens by mere coincidence to represent myself, has been assigned some properties, including my first and last names, and my (fictional) email address as well.

Nonetheless, thereís something else going on under the surface of the previous script. Once it finishes running, the destructor method is called, which saves the object to the predefined session variable. In doing so, itís possible to restore it on a different web page and keep handling it as one would do with regular session data.

Finally, feel free to edit all of the code samples included in this tutorial, which hopefully will arm you with a better grounding in building persistent objects in PHP 5.

Final thoughts

Thatís all for now. In this introductory chapter of the series, you learned the basics of creating persistent objects with PHP 5, which in this introduction to the subject used native sessions as the underlying persistent storage mechanism.

Also, itís valid to point out that the class that I created in this article isnít very flexible in its current state, because it permits you to use only a predefined session variable to store an instance of it. To overcome this issue, in the next tutorial Iím going to tweak its definition so that itíll be able to use any user-supplied session variable.

Donít miss the next part!

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: