PHP Object-oriented strategy (16) serialization of the _php Foundation

Source: Internet
Author: User
Tags constructor serialization
To serialize objects
Sometimes it is necessary to transfer an object to the network, in order to facilitate transmission, you can convert the entire object into a binary string,
When you reach the other end, revert to the original object, this process is called serialization, like we now want to put a car
The car was shipped to the United States by ship, because the car was larger, we could take the car apart into smaller parts, and then we shipped it round to the United States to assemble the parts back to the United States.
There are two situations in which we have to serialize objects, the first of which is to transfer an object to the network
The second scenario is to serialize an object to a file or database using serialization.
Serialization has two processes, one is serialization, is to convert objects into binary strings, we use
Serialize () function to serialize an object, the other is drag, that is, to convert the object into binary strings
To object, we use the unserialize () function to drag an object.
The parameter of the Serialize () function in PHP is the object name, the return value is a string, and the string returned by Serialize ()
Vague meaning, generally we do not parse this string to get the object's information, we simply pass the returned string
Go to the other end of the network or save it to a square piece.
The Unserialize () function in PHP Drag the Row object, and the function's argument is the return value of the Serialize () function.
The output is of course a well organized object. Code fragment
Copy Code code as follows:

?
Class person{
The following are the member properties of the person
var $name; The name of a man
var $sex; The gender of the person
var $age; The age of the person
Define a constructor parameter to assign 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 speak the way to speak his own attributes
function say () {
echo "My name is called:" $this->name. "Sex:". $this->sex. "My Age is:". $this->age. " <br> ";
}
}
$p 1=new person ("John", "Male", 20);
$p 1_string=serialize ($p 1); Serializing an object, returning a string
echo $p 1_string. " <br> "; Serialized strings We don't usually parse them.
$p 2=unserialize ($p 1_string); Drag the serialization of a serialized string to form an object $p2
$p 2->say ();
?>

The above example outputs the result:
Code fragment
O:6: "Person": 3:{s:4: "Name"; s:4: "John"; S:3: "Sex"; S:2: "Male"; S:3: "Age"; i:20;}
My name is called: John Sex: Male My age is: 20
In php5 there are two magic methods __sleep () methods and __wakeup () methods, which are called when the object is serialized.
A __sleep () method to accomplish some bedtime, and to wake up again, that is, to compose an object from a binary string
, you will automatically invoke another PHP function, __wakeup (), and do the actions that the object wakes up to do.
The __sleep () function does not accept any arguments, but returns an array that contains the attributes that need to be serialized. End to be wrapped
The included attributes will be ignored when serialized, and if there is no __sleep () method, PHP will save all properties.
Code fragment
Copy Code code as follows:

?
Class person{
The following are the member properties of the person
var $name; The name of a man
var $sex; The gender of the person
var $age; The age of the person
Define a constructor parameter to assign 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 speak the way to speak his own attributes function say ()
{
echo "My name is called:" $this->name. "Sex:". $this->sex. "My Age is:". $this->age. " <br> ";
}
Specifies that the $name and $age values in the returned array are serialized when serialized, ignoring the attributes that are not in the array $sex
function __sleep () {
$arr =array ("name", "Age");
return ($arr);
}
When the object is rebuilt, and the $age is assigned a value of 40
function __wakeup () {
$this->age = 40;
}
}
$p 1=new person ("John", "Male", 20);
Serializing an object, returning a string, calling the __sleep () method, ignoring the property not in the array $sex
$p 1_string=serialize ($p 1);
echo $p 1_string. " <br> "; Serialized strings We don't usually parse them.
$p 2=unserialize ($p 1_string); Drag the $p2 of the object, the $age is 40.
$p 2->say ();
?>

The previous example output value is:
Execution results
O:6: "Person": 2:{s:4: "Name", s:4: "John"; S:3: "Age"; i:20;
My name is called: John Sex: 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.