Page 1 of 1
Please help!! Is there a way to share object between page?
Posted: Mon May 25, 2009 4:06 pm
by yalag
I am new to php, but in other web technologies, you can share objects between page instances. For example, in java jsp pages you easily have on class that exist as static class for the whole server instance. How to do this in php?
Thank you and appreciate your help!!
Re: Please help!! Is there a way to share object between page?
Posted: Thu May 28, 2009 2:40 pm
by Darhazer
You can serialize the object in the session and retrieve it in the second page from the session
file1:
Code: Select all
session_start();
$myobj = new MyClass;
$_SESSION['myobj'] = $myobj;
file2:
Code: Select all
session_start();
// note: the definition of MyClass should be loaded before this line
// otherwise the object won't be deserialized
$myobj = $_SESSION['myobj'];
You can use the __sleep() and __wakeup() functions to predefine the serialization and deserialization.
Note: this is for all pages, but per user, not for all users. In JSP you have Application, which is shared between all users. To make this in PHP, you have to serialize the object in file or database, not in session, or to predefine the session handling, so you can use it across users