Several methods of serialization/deserialization of PHP

Source: Internet
Author: User

Serialization is the process of converting a variable into a string that can be saved or transmitted, and deserialization is the conversion of the string to the original variable at the appropriate time. Together, these two processes make it easy to store and transfer data, making the program more maintainable.

1. Serialize and Unserialize functions
<?PHP$a= Array ('a'='Apple','b'='Banana','C'='Coconut');    //Serializing Arrays$s=serialize ($a);echo $s;    //output: a:3:{s:1: "a"; S:5: "Apple"; s:1: "B"; S:6: "Banana"; s:1: "C"; s:7: "Coconut";}Echo'<br/><br/>';    //deserialization$o=unserialize ($s);Print_r ($o);    //output Array ([a] + Apple [b] = banana [c] = = Coconut)?>

Problems can occur when an array value contains characters such as double quotes, single quotes, or colons, which are deserialized. To overcome this problem, a clever trick is to use Base64_encode and Base64_decode.

$obj =Array ();    //Serialization of$s=Base64_encode (Serialize ($obj));    //deserialization$original=unserialize (Base64_decode ($s)), but base64 encoding increases the length of the string. To overcome this problem, you can use it with gzcompress.     //defines a function to serialize an objectfunction My_serialize ($obj)    {       returnBase64_encode (gzcompress (Serialize ($obj)));    }       //deserializationfunction My_unserialize ($txt)    {       returnunserialize (gzuncompress (Base64_decode ($txt)));    }
2. Json_encode and Json_decode

Serializing and deserializing using JSON format is a good choice:

    • Using Json_encode and Json_decode format output is much faster for serialize and unserialize formats.
    • The JSON format is readable.
    • The JSON format is smaller than the serialize return data result.
    • The JSON format is open and portable. It can also be used in other languages.
$a = Array ('a'='Apple','b'='Banana','C'='Coconut');    //Serializing Arrays$s=Json_encode ($a);echo $s;    //output: {"A": "Apple", "B": "Banana", "C": "Coconut"}Echo'<br/><br/>';    //deserialization$o= Json_decode ($s);

In the above example, the Json_encode output length is obviously shorter than the serialize output length in the previous example.

3. Var_export and Eval

The Var_export function outputs the variable as a string, and Eval executes the string as PHP code, deserializing the contents of the original variable.

$a = Array ('a'='Apple','b'='Banana','C'='Coconut');    //Serializing Arrays$s= Var_export ($a,true);echo $s;    //output: Array (' a ' + = ' Apple ', ' b ' = ' banana ', ' c ' = ' Coconut ',)Echo'<br/><br/>';    //deserializationEval ('$my _var='. $s.';');Print_r ($my _var);
4. Wddx_serialize_value and WDDX Deserialize

The Wddx_serialize_value function can serialize array variables and output them as XML strings.

$a = Array ('a'='Apple','b'='Banana','C'='Coconut');    //Serializing Arrays$s=Wddx_serialize_value ($a);echo $s;    //output (view source of output String): <wddxpacket version= ' 1.0 ' >Echo'<br/><br/>';    //deserialization$o=wddx_deserialize ($s);Print_r ($o);    //output: Array ([a] = Apple [b] = Banana 1 = Coconut)

As you can see, there are more XML tag characters, resulting in a lot of space for serialization of this format.

Summary

All of the above functions perform normally when serializing array variables, but they are different when applied to objects. For example, the Json_encode serialization object fails. When deserializing an object, Unserialize and eval will have different effects.

Several methods of serialization/deserialization of PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.