note 77826 added to function.unserialize
| From: | jsa1971 at gmail dot com | Date: | Fri, 14 Sep 2007 14:41:32 +0000 |
| Subject: | note 77826 added to function.unserialize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130493@lists.php.net to get a copy of this message | ||
Ignore all the advice to implement __autoload-ers or magical handlers, to solve problems when
retrieving class instances from sessions. It leads to terrible debugging nightmares.
After fussing with all sorts of such solutions and getting nowhere, I've found a very simple
solution that works correctly and explicitly:
$_SESSION['myInstance'] = serialize($myInstance);
....
$myInstance_s = @$SESSION['myInstance']
if (!is_null($myInstance_s)) {
$myInstance = unserialize($myInstance_s);
}
In other words, serialize the thing as a string, and put it in the session in this state - you
regain control of the unserialization process and can code to handle exception cases rather than
praying php's black magic does the right thing (and being sorely disappointed when it
doesn't).
----
Server IP: 209.41.74.194
Probable Submitter: 38.117.159.162
----
Manual Page -- http://www.php.net/manual/en/function.unserialize.php
Edit -- https://master.php.net/note/edit/77826
Del: integrated -- https://master.php.net/note/delete/77826/integrated
Del: useless -- https://master.php.net/note/delete/77826/useless
Del: bad code -- https://master.php.net/note/delete/77826/bad+code
Del: spam -- https://master.php.net/note/delete/77826/spam
Del: non-english -- https://master.php.net/note/delete/77826/non-english
Del: in docs -- https://master.php.net/note/delete/77826/in+docs
Del: other reasons-- https://master.php.net/note/delete/77826
Reject -- https://master.php.net/note/reject/77826
Search -- https://master.php.net/manage/user-notes.php