note 60356 deleted from function.unserialize by aharvey

From: Date: Tue, 06 Nov 2012 01:25:42 +0000
Subject: note 60356 deleted from function.unserialize by aharvey
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-191695@lists.php.net to get a copy of this message
Note Submitter: veg at rd dot no ---- 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.

« previous php.notes (#191695) next »