Re: Object can not be used after a session
| From: | Matt Silva | Date: | Wed, 16 Jul 2003 01:44:22 +0000 |
| Subject: | Re: Object can not be used after a session | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-155354@lists.php.net to get a copy of this message | ||
It turns out when I was assigning the object to the session var, the object was not getting
serialized. Serializing takes a variable and makes it into a string describing the variable and
the value. This could be done for any variable type except Resources (MySQL connection vars) and
Results (the result from a query).
Serialization happens automatically through the session_register() function, but you can only
use session_register if the the register_globals is turned on in the ini. I would like to move away
from the the practice of using globals and eventually turn off the register_globals.
For some reason I tried to register my $customer object using the session_register("customer") and
that seem to fail (not the actual registering, but the reuse of the object) so now this is a solution that I
saw in the php documentation:
$customer = new Customer($_GET['facilityID'], $_GET['customerID']);
$_SESSION['acceptPayment']['serializedCustomer'] = serialize($customer);
so now when I have moved on to another page or another instance of the same page and
I want to access the object from the session var, I do so like this:
$customer = unserialize($_SESSION['acceptPayment']['serializedCustomer']);
and now you can access the object. There is a hidden jewl about this method, I now no longer
have to include or require the class file because it is already defined in the serialized string.
Matt
Matt Silva wrote:
Hi I was wondering if anybody has any Ideas about or has experienced this.... I create a new object and then assign it to a session var $customer = new Customer($_GET['facilityID'], $_GET['customerID']); $_SESSION['acceptPayment']['customer'] = $customer; but later when I access that session var [in the the same file but in a different function and different instance], php gives me an error saying: "The script tried to execute a methode or access a property of an incomplete object. Pleas ensure that the class definition lt;bgt;customerlt;/bgt; of the object you are trying to operate on was loaded _before_ the session was started at <filepath>/acceptPayment.php line 103" I did a little reading (rtfm) on php.net and saw that if session.auto_start is turned on, you couldn't use Objects with sessions. Well I checked my ini and the session.auto_start was set to 0, so I am now scratching my head in confusion. I require_once the Customer class and start the session after the requires and includes, so then I thought ok require_once so I change it to require and then it doesn't load the class the for some reason (sigh). Thanks for your help in advance Matt