It is difficult to store objects in sessions as PHP doesn't necessarily have the correct class definitions on hand to unserialize the objects. Below is a simple session class that can act as a container for objects, but you have to pass the path to the class definition for this to work...
Assume you have this class defined in Session.php:
class Session
{
# attributes
var $_objects; # hash of named objects- of anything you want! Woot!
var $_classes; # hash of classes- This should import all classes required for objects in the session
var $_saved_objects;
function Session()
{
if (isset($_SESSION['__SESSSION_GLOBAL_OBJ']))
{$this=$_SESSION['__SESSSION_GLOBAL_OBJ'];}
else
{
$this->_objects=array();
$this->_classes=array();
$this->_saved_objects=array();
$_SESSION['__SESSSION_GLOBAL_OBJ']=$this;
}
}
function setObject($name,&$object/*, Required Class Paths */)
{
for ($i=2;$i<func_num_args();$i++)
{
if (!in_array(func_get_arg($i),$this->_classes))
{$this->_classes[]=func_get_arg($i);}
}
$this->_objects[$name]=&$object;
}
function getObject($name)
{
if (isset($this->_objects[$name]))
{return $this->_objects[$name];}
return false;
}
function __sleep()
{
# Serialize all objects
$keys = array_keys($this->_objects);
for ($i=0;$i<count($keys);$i++)
{$this->_saved_objects[$keys[$i]] = serialize($this->_objects[$keys[$i]]);}
return array("_saved_forms","_saved_objects","_classes");
}
function __wakeup()
{
# include all classes needed to unserialize objects in this session
for ($i=0;$i<count($this->_classes);$i++)
{include_once($this->_classes[$i]);}
# unserialize objects
$keys = array_keys($this->_saved_objects);
for ($i=0;$i<count($keys);$i++)
{
$this->_objects[$keys[$i]] = unserialize($this->_saved_objects[$keys[$i]]);
unset($this->_saved_objects[$keys[$i]]);
}
}
}
session_start();
if (!isset($_SESSION['__SESSSION_GLOBAL_OBJ']))
{$_SESSION['__SESSSION_GLOBAL_OBJ'] = new MSSession();}
$SESSION = &$_SESSION['__SESSSION_GLOBAL_OBJ'];
=========================================================
Now assume you have another class called MyClass defined in MyClass.php.... you can store it in the session as such:
include_once('Session.php');
include_once('stuff/MyClass.php');
$joe = new MyClass(/*arguments*/);
$SESSION->setObject('joe',$joe,'stuff/MyClass.php');
=========================================================
On another page you can retrieve this object from the session...
include_once('Session.php');
$joe = &$SESSION->getObject('joe');
=========================================================
This works, but you HAVE to pass setObject the path of every class definition you need to use the object you're embedding.