Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is marshaling/ serialization in PHP as simple as serialize($var)?

here's a definition of marshaling from Wikipedia:

In computer science, marshalling (similar to serialization) is the process of transforming the memory representation of an object to a data format suitable for storage or transmission. It is typically used when data must be moved between different parts of a computer program or from one program to another.

I have always done data serialization in php via its serialize function, usually on objects or arrays. But how is wikipedia's definition of marshaling/serialization takes place in this serizalize() function?

like image 455
yretuta Avatar asked Apr 10 '10 22:04

yretuta


1 Answers

What serialize doesn't do is transport class definitions. When unserializing an object, that object's class definition must be present (loaded from the code base), otherwise unserializing will fail. From the Wikipedia article you mention:

To "marshal" an object means to record its state and codebase(s) in such a way that when the marshalled object is "unmarshalled", a copy of the original object is obtained, possibly by automatically loading the class definitions of the object. You can marshal any object that is serializable or remote. Marshalling is like serialization, except marshalling also records codebases. Marshalling is different from serialization in that marshalling treats remote objects specially.

If I understand correctly, Serialize is definitely not 100% compatible with the definition of marshaling in that respect. I don't know a pre-defined mechanism that would do this in PHP. I guess you would have to combine the serialized data and all necessary class definitions into a package (a ZIP file for example).

like image 82
Pekka Avatar answered Oct 13 '22 01:10

Pekka