note 117589 added to function.unserialize

From: Date: Thu, 02 Jul 2015 15:28:14 +0000
Subject: note 117589 added to function.unserialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-203029@lists.php.net to get a copy of this message
When you serialize an object of a class from a particular namespace, the namespace is recorded as part of the serialization. If you decide to change this namespace's name, it can be hard to read in old serialized objects. I.e., suppose you had serialized an object of type foo\A, you change the namespace of your project to goo but otherwise leave the class definition of A unchanged. You would like to be able to unserialize the object as goo\A, instead unserialization will only create a partial object. To fix this in the case where you don't have nested objects in your class definition, you can use the following simple rename function: /** * Used to change the namespace of a serialized php object (assumes doesn't * have nested subobjects) * * @param string $class_name new fully qualified name with namespace * @param string $object_string serialized object * * @return string serialized object with new name */ function renameSerializedObject($class_name, $object_string) { /* number of digits in the length of name of the object needs to be less than 12 digits (probably more like 4) for this to work. */ $name_length = intval(substr($object_string, 2, 14)); $name_space_info_length = strlen("O:".$name_length.":") + $name_length + 2; // 2 for quotes; $object_string = 'O:' . strlen($class_name) . ':"'. $class_name.'"' . substr($object_string, $name_space_info_length); return $object_string; } ---- Server IP: 72.52.91.14 Probable Submitter: 173.13.143.78 ---- Manual Page -- http://php.net/manual/en/function.unserialize.php Edit -- https://master.php.net/note/edit/117589 Del: integrated -- https://master.php.net/note/delete/117589/integrated Del: useless -- https://master.php.net/note/delete/117589/useless Del: bad code -- https://master.php.net/note/delete/117589/bad+code Del: spam -- https://master.php.net/note/delete/117589/spam Del: non-english -- https://master.php.net/note/delete/117589/non-english Del: in docs -- https://master.php.net/note/delete/117589/in+docs Del: other reasons-- https://master.php.net/note/delete/117589 Reject -- https://master.php.net/note/reject/117589 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#203029) next »