Porting PHP serialization from PHP 5.3 to PHP 5.6

I have a complex structure that is stored in a MySQL database using the serialize () function and then converted back using unserialize (). After migrating the system from PHP 5.3 to PHP 5.6 and deserializing in 5.6 the data that was serialized in 5.3, the structures were damaged. Some object references are now displayed as arrays.

My questions:

  1. Is there a spec about the different coding used for serialization / deserialization in different versions of PHP? (I can not find anything specific enough in my Google searches or in the documentation on PHP.net)

  2. How to convert serialized data from PHP 5.3 encoding to PHP 5.6 encoding?

+9
php serialization migration
source share
2 answers

Yes, object serialization has been changed in PHP5.6. In particular, in PHP5.6 several areas related to object serialization were associated.

There is an undefined note about this in the PHP unserialize manual :

5.6.0 Manipulating serialized data by replacing C: with O: forcing the creation of an object without calling the constructor will now fail.

However, viewing the list of errors shows that a little more report 68099 appeared under the hood. It also indicates that there is no official documentation in the original format:

"the original behavior (we allow the old serialization format used for classes using the new format) was never documented, officially supported",

Please note that the end result of this discussion was β€œWill not be fixed . ”

Basically your options are:

  • Try using one of the other serializers as a way to export data between versions of PHP. For example, session_encode , which can also process objects.

  • Conversion script. There is a widely documented version of the current PHP intern format, which you could use with an iterator in the old format to update the syntax.

+4
source share

You can convert serialized data to JSON (using the PHP 5.3 installation), save it in the database and then do the opposite (using the PHP 5.6 installation).

5.3 to JSON:

$data = unserialize($strSerializedData); $jsonData = json_encode($data); 

From JSON to 5.6:

 $data = json_decode($jsonData); $strSerializedData= serialize($data ); 

You may need to adjust the parameters that you send to json_decode according to the source data.

This parameter will depend on what data is being serialized. If your data is classes, this will not work.

In addition, the problem may be related to this note in the documentation ( here ):

5.6.0 Manipulating serialized data by replacing C: with O: forcing the instantiation of an object without calling the constructor will now fail.

+1
source share

All Articles