Sessions and Objects
| From: | James Du Russel | Date: | Fri, 16 Jun 2000 23:56:55 +0000 |
| Subject: | Sessions and Objects | ||
| Groups: | php.dev php.general | ||
| Request: | Send a blank email to php-dev+get-21575@lists.php.net to get a copy of this message | ||
When PHP 4 restores objects from a session, if the class is not defined,
the object type reverts to 'stdClass' As long as session_start()
happens belows class definitions, the code works fine.
Here's the problem. Page A creates an object of some type, and stores
it into the session. The user works with page A for a while, then moves
over to page B. Page B has no requirement for the object created by
page A, but the session data is required (for the logged in user object)
When page B loads the session data, it finds an object it does not
recognize, and thus sets its type to stdClass. When page B is finished,
it stores the object back into the session, as stdClass! If the user
goes back to page A, the methods on the object are gone, and lo the page
is broken!
As an alternative to loading the code for an entire site, I'm
considering all manner of really ugly serialize/ereg/unserialize tricks
to work around this. Does anyone have an suggestions for a workaround.
For the phpdev people, can someone fix this? Better behavior would be
to preserve the class name, but probably means a lot of internal hacks.
Alternatly, can someone write (I've asked for this before) a simple
function to force an object into a certain type? It would also be nice
for it to return the current/old type of an object as well. I envision
it something like this...
string object_type(object ob, [string newtype])
James