In PHP I'm having a real hard time using serialize
/unserialize
on a large array of objects (100000+ objects). These objects can be of a lot of different types, but are all descendants from a base class.
Somehow when I use unserialize
on the array of objects about 0,001% of the objects are generated wrong! A whole different object is generated instead. This does not happen random, but each time with the same objects. But if I change the order of the array, it happens with different objects, so this looks like a bug to me.
I switched to json_encode
/json_decode
, but found that this always uses stdClass
as the object's class. I solved this by including the classname of each object as a property, and then use this property to construct a the new object, but this solution is not very elegant.
Using var_export
with eval
works fine but is about 3 times slower than the other methods and uses much more memory.
Now my questions are :
- what could cause the bug / wrong objects that are created with
unserialize
? - is there a better way to use
json_decode
with an array of objects, so that classes are somehow stored within the json automatically? - is there maybe even an other method to read/write a large array of objects in PHP?
UPDATE
I'm beginning to believe there must be something strange with my array-data, because with msgpack_serialize
(php extension, alternative to serialize
) I get the same kind of errors (but strangely enough not the same objects are generated wrong!).
UPDATE 2
Found a solution : instead of doing serialize
on the entire array, I do it on each object now, first serialize
and then base64_encode
and then I store each serialized object as a separate line in a text-file. This way I can generate the entire array of objects and then iterate each object using file()
with unserialize
and base64_decode
: no more errors!