PHP Serialization and deserialization are the same as java serialization. Java only needs to implement the empty Serializable interface. Serialize () encodes variables and their & #20540; into text form unserialize () restore original variables when serialization is required when you want to serialize PHP) and deserialization
This is the same as the java sequence. Java only needs to implement the empty Serializable interface.
Serialize () encodes variables and their values into text form
Unserialize () restore original variable
Under what conditions should we serialize the data when you want to write the objects in the memory to the hard disk database; when you want to transfer objects on the network;
When the serialized data is placed in a URL and transmitted between pages, you need to call urlencode () to ensure that the URL metacharacters in the URL are processed.
The settings of the margic_quotes_gpc and magic_quotes_runtime parameters affect the data transmitted to unserialize. If magic_quotes_gpc is enabled, the data transmitted in URL, POST variables, and cookies must be processed using stripslashes () before deserialization. if magic_quotes_runtime is enabled, before writing serialized data to a file, you must use addslashes () for processing. before reading these data, you must use stripslashes () for processing:
Array can also be used to serialize an array object.
Index = $ index; $ this-> name = $ name ;}$ data1 = new Data (1, "hello"); $ data2 = new Data (2, "world"); $ arr = array (); // you can use ArrayObject. // $ Arr = new ArrayObject (); $ arr [0] = $ data1; $ arr [1] = $ data2; $ str = serialize ($ arr ); $ file = fopen ("tmp.txt", "w"); fwrite ($ file, $ str); fclose ($ file); // $ file = fopen ("tmp.txt ", "r"); $ data = file_get_contents ("tmp.txt"); // deserialize the original array object. $ Obj = unserialize ($ data); print_r ($ obj [0]); echo $ obj [0]-> name;?>
The content of tmp.txt is:
a:2:{i:0;O:4:"Data":2:{s:5:"index";i:1;s:4:"name";s:5:"hello";}i:1;O:4:"Data":2:{s:5:"index";i:2;s:4:"name";s:5:"world";}}