Page 1 of 1
$_SESSION[Object] is ok?
Posted: Thu Nov 01, 2007 1:17 am
by dwnthk
Hi there,
I have a class called PageImp.
In the first page, I have a variable called $sopage which got the type of PageImp class.
***** PLEASE USE TAGS WHEN POSTING CODE *****
Code: Select all
$sopage = new PageImp();
$_SESSION['sopage'] = $sopage;
......
In the other page, I try to retrieve the object like:
Code: Select all
$so2page = new PageImp();
$so2page = $_SESSION['sopage'];
.....
I got $so2page empty.
Can I pass the object in this way? If I can't, how can I pass the object between pages?
Thanks.
Dave
Posted: Thu Nov 01, 2007 9:24 am
by Kieran Huggins
Code: Select all
if(!isset($_SESSION['sopage'])) $_SESSION['sopage'] = new PageImp();
should work both places.
Re: $_SESSION[Object] is ok?
Posted: Thu Nov 01, 2007 11:19 am
by Christopher
Remember, both sessions and objects need something before they can be used. And if you store object in the session then the class needs to exist before the session is started.
Code: Select all
include '/path/to/PageImp.php';
session_start();
$sopage = new PageImp();
$_SESSION['sopage'] = $sopage;
and:
Code: Select all
include '/path/to/PageImp.php';
session_start();
$so2page = $_SESSION['sopage'];
Re: $_SESSION[Object] is ok?
Posted: Fri Nov 02, 2007 12:02 am
by dwnthk
arborint wrote:Remember, both sessions and objects need something before they can be used. And if you store object in the session then the class needs to exist before the session is started.
Code: Select all
include '/path/to/PageImp.php';
session_start();
$sopage = new PageImp();
$_SESSION['sopage'] = $sopage;
and:
Code: Select all
include '/path/to/PageImp.php';
session_start();
$so2page = $_SESSION['sopage'];
Thanks. It works.

I am not sure why I don't need to create the $so2page before I can assign the object to it. It is the way of PHP does maybe?!
Dave
Re: $_SESSION[Object] is ok?
Posted: Fri Nov 02, 2007 1:51 am
by Christopher
dwnthk wrote:I am not sure why I don't need to create the $so2page before I can assign the object to it. It is the way of PHP does maybe?!
You create the object in the first script. You assign it into the $_SESSION superglobal array. So when the first script ends, PHP serialize()'s all the data in $_SESSION and writes the data to whatever datastore your session is using (to disk by default). For objects is saves the class name.
Then in the second script when you start the session it reads the data from the datastore and unserialize()'s it. If there is an object in the data it checks to see if the class is loaded and if it is, it fixes up the object just like new. If the class is not loaded it marks the object class as unknown.
Posted: Tue Nov 06, 2007 9:07 pm
by dwnthk
I got this error message when I tried to pass the object to the other script:
Code: Select all
Fatal error: main() [<a href='function.main'>function.main</a>]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "PageImp" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in C:\Program Files\Apache Group\Apache2\htdocs\test\pmc_test1.php on line 30
I did not do any serialize() function in my php. Is that because the php will do serialize() by itself?
After that, then I tried to add serialize() and unserialize() to my scripts, but it seems like serialize()/unserialize() only take string as the parameter. Is it correct?
Thanks.
Posted: Tue Nov 06, 2007 9:26 pm
by dwnthk
Gezze... It seems working now. What I did is to swap the following lines:
Code: Select all
session_start();
include ('PageImp.php');
