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