faqts : Computers : Programming : Languages : PHP : kms

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

53 of 64 people (83%) answered Yes
Recently 5 of 10 people (50%) answered Yes

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