Entry
How can I make an object (an instance of a class I built) persistent so that I can use it in diferent php pages?
Jun 6th, 2002 01:22
Luc Saffre, Diez B. Roggisch, Carlos Santos,
You can simply store it into a session. I use code like this:
include("object.php");
if(isset($object_ser))
$object = unserialize($object_ser);
else
{
$object = new Object;
session_register("object_ser");
$object_Ser = serialize($object);
}
Always include the objects definition before unserializing, otherwise
PHP doesn't konow how to create the object. And it might be that
serializing isn't required at all, if you put the include before your
session_start(), because then the object will be instantiated in its
session-var by PHP itself. But this can cause trouble if your includes
prevent the creation of the session.
Diez B. Roggisch
Note that in PHP 4, all this becomes automatic if you put your object
instance into $_SESSION.
http://ee.php.net/manual/en/language.oop.serialization.php
Luc Saffre