note 33941 added to language.oop.magic-functions
| From: | mccann at cs dot usm dot maine dot edu | Date: | Fri, 11 Jul 2003 15:38:25 +0000 |
| Subject: | note 33941 added to language.oop.magic-functions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-51918@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/language.oop.magic-functions.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33941
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33941&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33941&report=yes