PHP Object-oriented-object serialization serialize (), __sleep (), __wakeup () code

Source: Internet
Author: User
Tags object serialization
Sometimes you need to transfer an object on the network, in order to facilitate the transfer, the whole object can be converted into a binary string, and then to the other end, and then revert to the original object, this process is called serialization (also known as serialized) serialization, as we now want to transport a car through the ship to the United States, Because of the size of the car, we can take the car apart into small parts, and then we ship the parts to the United States by wheel, and then assemble the parts back into the car.
In layman's terms, serialization is a practice of converting the "memory" data represented by a variable into a "string" data form and persisting on a hard disk, and deserialization is a reverse process.
There are two cases where we have to serialize objects, the first is to serialize an object when it is being transmitted over the network, and the second is to serialize the object when it is written to a file or database.
Serialization has two processes, one is serialization, is to convert the object into a binary string, we use the Serialize () function to serialize an object, the other is crossdress (also known as deserialization), is to convert the object of the binary string into an object, We use the Unserialize () function to crossdress an object.
The parameters of the Serialize () function in PHP are the object name, the return value is a string, the string returned by Serialize () is ambiguous, and generally we do not parse the string to get the information of the object, we just need to upload the returned string to the other end of the network or save it to a file.
The Unserialize () function in PHP crossdress the line object, which is the return value of the Serialize () function, and the output is, of course, a re-organized object.
  
serialization of the object:

    1. Serializing an object can only "save" its property data, and the method is ignored (the method is not data)

    2. When the object is serialized, the Magic method of the class to which the object belongs is automatically called: Sleep ()

Deserialization of the object:

    1. To deserialize an object is actually the property data that was saved in the recovery period, and it is necessary to rely on the original class of the object.

    2. When the object is deserialized, it automatically calls the Magic method of the class that the object belongs to: Wakeup ()

All serialization

1.serialize (object name)

Serialize the specified class object $str =serialize ($per)//serialize the Per object and return the result to $str
 
Serialization Practices:

$v 1 = 123; This is a variable that represents arbitrary memory data $S1 = Serialize ($v 1);  Convert any type of variable data to "string" file_put_contents (' Target text file to save ', $s 1);    Save the string to a file (that is, hard disk data)

2.unserialize (serialized return value)

Crossdress, the returned result is the object $per =unserialize ($STR);

Deserialization procedure:

$s 1 = file_get_contens (' Save target text file for serialized data ');    Read all of the characters from a file $v1 = Unserialize ($s 1);   Convert the string data, deserialize to a variable (data)

Examples of serialization and deserialization

<?phpclass person{    var $name;       var $sex;        var $age;        function construct ($name = "", $sex = "", $age = "") {        $this->name = $name;                $this->sex = $sex;                $this->age = $age;    }    function say () {        echo "My name is:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age. "<br>";    }} $p 1 = new Person ("Zhang San", "male"), $p 1_string = serialize ($p 1);    Serialize an object and return a string echo $p 1_string. "<br>";        Serialized strings We usually do not parse//store $p1_string into file file.txt file_put_contents ('./file.txt ', $p 1_string); $p 2 = unserialize ($p 1_ string);    Crossdress a serialized string into an object $p2$p2->say ();//The following procedure is the same as the above effect $p3_file = file_get_contents ('./file.txt '); Read file $P3 = unserialize ($p 3_file); Deserialization $p3->say ();? >

The above example outputs the result:

O:6: "Person": 3:{s:4: "Name", s:4: "Zhang San"; s:3: "Sex"; S:2: "Male"; S:3: "Age"; i:20;} My name is: Zhang San Sex: Male my age is: 20

Local serialization

1.sleep ()

Serialize part of an object's properties.

2.wakeup ()

Initialize (actually modify) the object contents when crossdress.
There are two magic methods in PHP5 the Sleep () method and the Wakeup () method, which, when the object is serialized, invokes a sleep () method to do some things before bedtime, and when you wake up, that is, when the binary string is re-formed into an object, will automatically call another PHP function wakeup (), do some of the objects woke up to do the action. The sleep () function does not accept any arguments, but returns an array containing the properties that need to be serialized. At the end of the included property will be ignored at serialization, and if there is no sleep () method, PHP will save all properties.

<?class person{    var $name;       var $sex;        var $age;        function construct ($name = "", $sex = "", $age = "") {        $this->name = $name;                $this->sex = $sex;                $this->age = $age;    }    function say () {        echo "My name is:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age. "<br>";    }    Specifies serialization when serializing the $name and $age values in the returned array, ignoring attributes that are not in the array $sex    function sleep () {        $arr = array ("name", "Age");//At this time, the property $ Sex will be removed!!!        return ($arr);    }    When the object is regenerated and re-assigned the value $age to    wakeup () {        $this->age =;    }} $p 1 = new Person ("Zhang San", "male", 20);//Serialize an object, return a string, call the Sleep () method, ignore the property not in the array $sex$p1_string = serialize ($p 1); Echo $p 1_ String. "<br>"; Serialized strings We do not usually parse $p2 = unserialize ($p 1_string); crossdress $p2 re-assignment $age to 40$p2->say (); >

The above example output is:

o:6: "Person": 2:{s:4: "Name", s:4: "Zhang San"; S:3: "Age"; i:20;} My name is: Zhang San Sex: My age is: + 

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.