note 77826 modified in function.unserialize by aharvey
| From: | aharvey@php.net | Date: | Tue, 06 Nov 2012 01:33:41 +0000 |
| Subject: | note 77826 modified in function.unserialize by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191713@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).
--was--
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).
http://php.net/manual/en/function.unserialize.php