note 60356 added to function.unserialize

From: Date: Wed, 04 Jan 2006 18:34:43 +0000
Subject: note 60356 added to function.unserialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-101212@lists.php.net to get a copy of this message
Unserializing an object of a class that hasn't been declared where it is being unserialized will result in an unusable object. I do not know why this happens, but I know of two workarounds which I've written about here; http://vega.rd.no/entry/200601_serializing_objects_in_php In short, the solution is to declare the class before unserializing the object; so this code will do when unserializing an object of type MyClass; <?php class MyClass {} $object = unserialize($serializedData); ?> However, if you have several different classes, which you don't necessarily know the names of beforehand, use this short function to handle it for you; <?php function unserializeUnknownObject($text) { $matches = Array(); preg_match_all('/O:(\d)*:"([^":]*)":/', $text, $matches); foreach ($matches[2] as $class) { eval('class '.$class.' { }'); } return unserialize($text); } $object = unserializeUnknownObject($serializedData); ?> This could also have been accomplished by using unserialize_callback_func. ---- Manual Page -- http://www.php.net/manual/en/function.unserialize.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60356 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60356&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60356&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60356&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60356&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60356&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60356&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60356&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60356&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#101212) next »