Home arrow PHP arrow Page 4 - Persistent Objects and Different Session Variables in PHP 5

Building a wrapping example - PHP

Despite its rather intimidating name, persistent objects represent a pretty simple concept applied very often in software development. It's aimed at creating structured entities that can maintain their status across different stages of an application. This six-part series will take the mystery out of persistent objects so you can use them in your own applications.

TABLE OF CONTENTS:
  1. Persistent Objects and Different Session Variables in PHP 5
  2. Building a basic persistent class in PHP 5
  3. Saving class instances to different session variables
  4. Building a wrapping example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
September 21, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To demonstrate how an instance of the enhanced “User” class can persist across two HTTP requests, I’m going to define a couple of PHP files. The first one will create a new user object, and the second will assign new values to its properties, once the object has been reloaded via its factory method.

Does this sound rather confusing to you? Fear not; just look at the definition corresponding to the first of these files:

<?php

 

class User

{

private $data = array();

private static $key = 'user';

 

// constructor (not implemented)

public function __construct(){}

 

// factory method

public static function factory($key = '')

{

session_start();

if ($key != '')

{

self::$key = $key;

}

if(isset($_SESSION[self::$key]) === TRUE)

{

return unserialize($_SESSION[self::$key]);

}

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[self::$key] = serialize($this);

}

}

 

// example on creating a persistent object

$user = User::factory('newuser');

$user->fname = 'Alejandro';

$user->lname = 'Gervasio';

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

 

?>

Frankly speaking, nothing spectacular is happening here. The previous file shows how to create a brand new user object, which, thanks to the enhanced functionality of its originating class, is now stored on a different session variable called $_SESSION['newuser'].

On the other hand, it’s necessary to create a second file that retrieves this object and modifies its properties as well. The one below does that in a simple manner:

<?php

 

class User

{

private $data = array();

private static $key = 'user';

 

// constructor (not implemented)

public function __construct(){}

 

// factory method

public static function factory($key = '')

{

session_start();

if ($key != '')

{

self::$key = $key;

}

 

if(isset($_SESSION[self::$key]) === TRUE)

{

return unserialize($_SESSION[self::$key]);

}

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[self::$key] = serialize($this);

 }

}

 

// assign new values to properties of the persistent object

$user = User::factory('newuser');

$user->fname = 'John';

$user->lname = 'Doe';

$user->email = 'john@domain.com';

 

?>

As I expressed earlier, this second file simply reloads the user object created earlier via its factory method, and then assigns some new values to the corresponding properties. Finally, the new state of the object is saved internally to the specified session variable by means of its destructor, which demonstrates in a nutshell how easy it is to extend the functionality of the persistent “User” class.

And with this last example I’m finishing this article. As usual, feel free to edit all of the code samples shown in this tutorial, which hopefully will give you a more solid background in building persistent objects with PHP 5.

Final thoughts 

That’s all for now. In this second part of the series, I demonstrated how to build a persistent class that has the ability to save its instances to different session variables. As you saw for yourself, building this particular class was pretty straightforward, so in theory you shouldn’t have major problems replicating it on your own testing web server.

Moving forward, in the next tutorial I’m going to discuss how to create persistent objects that use text files as their default storage mechanism, unlike simple cookies. So, now that you know what to expect from the forthcoming part of this series, you can’t miss it!



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap

Dev Shed Tutorial Topics: