Php object-oriented full strategy (16) serialization of objects

Source: Internet
Author: User
Sometimes an object needs to be transmitted over the network. to facilitate transmission, the entire object can be converted into a binary string and serialized.
Sometimes an object needs to be transmitted over the network. to facilitate transmission, the entire object can be converted into a binary string,
When it reaches the other end, it is restored to the original object. this process is called serialization, just as we want
A car is shipped to the United States by ship. because the size of the car is large, we can split the car into small parts, and then we will ship these parts to the United States in a round, in the United States, these parts are assembled back into cars.
In either case, we must serialize objects. in the first case, when an object is transmitted over the network
Serializes objects. The second case is to serialize objects when they are written to a file or database.
There are two steps of serialization. one is serialization, which is to convert an object into a binary string. We use
The serialize () function serializes an object, and the other is deserialization, which is to convert the binary string converted by the object
As an object, we use the unserialize () function to deserialize an object.
The parameter of the serialize () function in PHP is the object name, the returned value is a string, and the string returned by Serialize ()
Fuzzy meaning. generally, we do not parse this string to obtain the object information. we only need to pass the returned string
To the other end of the network or save it to the component.
In PHP, the unserialize () function is used to deserialize objects. the parameter of this function is the return value of the serialize () function,
The output is of course a re-organized object. Code snippet

The code is as follows:


Class Person {
// The following are the member attributes of a person.
Var $ name; // name of a person
Var $ sex; // gender of a person
Var $ age; // age of a person
// Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age.
Function _ construct ($ name = "", $ sex = "", $ age = ""){
$ This-> name = $ name;
$ This-> sex = $ sex;
$ This-> age = $ age;
}
// This person can talk about his/her attributes.
Function say (){
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";
}
}
$ P1 = new Person ("zhang san", "male", 20 );
$ P1_string = serialize ($ p1); // serializes an object and returns a string
Echo $ p1_string ."
"; // Serialized strings are not parsed.
$ P2 = unserialize ($ p1_string); // concatenates a serialized string to form an object. $ p2
$ P2-> say ();
?>


Output result of the preceding example:
Code snippet
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 John. Gender: Male. my age is: 20.
There are two magic methods _ sleep () and _ wakeup () in php5. when the object is serialized
One _ sleep () method is used to complete some bedtime tasks. while waking up again, the binary string re-forms an object.
Then, another function _ wakeup () of PHP is automatically called to do some actions that will be done when the object wakes up.
The _ sleep () function does not accept any parameters, but returns an array containing serialized attributes. Last quilt
Attributes are ignored during serialization. if The _ sleep () method is not available, PHP will save all attributes.
Code snippet

The code is as follows:


Class Person {
// The following are the member attributes of a person.
Var $ name; // name of a person
Var $ sex; // gender of a person
Var $ age; // age of a person
// Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age.
Function _ construct ($ name = "", $ sex = "", $ age = ""){
$ This-> name = $ name;
$ This-> sex = $ sex;
$ This-> age = $ age;
}
// The way this person can talk, say his own attribute function say ()
{
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";
}
// Specify the $ name and $ age values in the returned array for serialization, ignoring the attribute $ sex not in the array
Function _ sleep (){
$ Arr = array ("name", "age ");
Return ($ arr );
}
// When the object is regenerated, the value of $ age is 40.
Function _ wakeup (){
$ This-> age = 40;
}
}
$ P1 = new Person ("zhang san", "male", 20 );
// Serializes an object and returns a string. The _ sleep () method is called to ignore the attribute $ sex that is not in the array.
$ P1_string = serialize ($ p1 );
Echo $ p1_string ."
"; // Serialized strings are not parsed.
$ P2 = unserialize ($ p1_string); // form an object through deserialization. $ p2 is assigned a value again. $ age is 40.
$ P2-> say ();
?>


The output value of the preceding example is:
Execution result
O: 6: "Person": 2: {s: 4: "name"; s: 4: "Zhang San"; s: 3: "age"; I: 20 ;}
My name is Zhang San gender: My age is: 40

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.