"Getting Started with PHP object-oriented (OOP) Programming" 22. Serialization of Objects serialize () method, __sleep () method, __wakeup () method

Source: Internet
Author: User
Tags getting started with php

Sometimes you need to transfer an object on the network, in order to facilitate the transfer, the entire object can be converted into a binary string, and then to the other end, then revert to the original object , this process is called serialization (also known as serialized), It's like we're going to ship a car to the United States by ship, because the car is bigger, we can take the car into smaller parts, and then we ship the parts to the United States by wheel, and then we assemble the parts back into the car.

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 procedures, one is serialization , is to convert the object into a binary string, we use the serialize () function to serialize one object, the other is crossdress , is to convert the binary string of the object 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.

<?class person{//Below is a member of the human attribute var $name;//person's name Var $sex;//Human gender var $age;//person's age//define a constructor method parameter for property name $name, gender $sex and age $ Age assigns function __construct ($name = "", $sex = "", $age = "") {$this->name = $name; $this->sex = $sex; $this->age = $age;} This person can speak in a way that speaks his own attribute function say () {echo "My name is called:". $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 to return a string echo $p 1_string. "<br>";//serialization of strings we usually do not parse $P2 = unserialize ($p 1_string);//Serialize a serialized string crossdress to form an object $p2$p2->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 called: Zhang San Sex: Male my age is:

There are two magic methods in PHP5 __sleep () method and __wakeup () method, when the object is serialized, a __sleep () method is called to complete some things before bedtime, and when you wake up, that is, 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 that contains the properties that need to be serialized. At the end of the contained property will be ignored at serialization, and if there is no __sleep () method, PHP will save all properties.

<?class person{//Below is a member of the human attribute var $name;//person's name Var $sex;//Human gender var $age;//person's age//define a constructor method parameter for property name $name, gender $sex and age $ Age assigns function __construct ($name = "", $sex = "", $age = "") {$this->name = $name; $this->sex = $sex; $this->age = $age;} This person can speak in a way that speaks his own attribute function say () {echo "My name is called:". $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 $sexfunction __sleep () {$arr = array ("name", "Age");//At this time, the property $sex will be deleted!!! return ($arr);} When the object is regenerated and re-assigned the value $age to 40function __wakeup () {$this->age = 40;}} $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 output value of the above example is:

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

"Getting Started with PHP object-oriented (OOP) Programming" 22. Serialization of Objects serialize () method, __sleep () method, __wakeup () method

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.