Home > Backend Development > PHP Tutorial > Learning experience from the Third Plenary Session of the 18th CPC Central Committee Section 13 - Object serialization

Learning experience from the Third Plenary Session of the 18th CPC Central Committee Section 13 - Object serialization

WBOY
Release: 2016-07-29 08:35:00
Original
856 people have browsed it

/*
+------------------------------------------------ ----------------------------------+
| = This article is read by Haohappy<> ;
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid unnecessary trouble that may occur, please do not reprint, thank you
| = Welcome to criticize and correct, hope and all PHP enthusiasts Make progress together!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+-------------------------- -------------------------------------------------- ---+
*/
Section 13--Object Serialization
Serialization can convert variables, including objects, into continuous bytes data. You can save the serialized variables in a file or on the network and then deserialize it back to the original data. For the class you define before deserializing the object of the class, PHP can successfully store the properties and methods of its object. Sometimes you may need an object to be deserialized Executed immediately after serialization. For this purpose, PHP will automatically look for the __sleep and __wakeup methods.
When an object is serialized, PHP will call the __sleep method (if it exists). Before deserializing an object Finally, PHP will call the __wakeup method. Neither method accepts parameters. The __sleep method must return an array containing the properties that need to be serialized. PHP will discard the values ​​​​of other properties. If there is no __sleep method, PHP will save All attributes.
Example 6.16 shows how to serialize an object using the __sleep and __wakeup methods. The Id attribute is a temporary attribute that is not intended to be retained in the object. The __sleep method guarantees that the id is not included in the serialized object Properties. When deserializing a User object, the __wakeup method establishes a new value for the id property. This example is designed to be self-sustaining. In actual development, you may find that objects containing resources (such as images or data streams) require these methods. .
Listing 6.16 Object serialization

Copy code The code is as follows:

class User
{
public $name;
public $id;
function __construct()
{
//give user a unique ID gives a different ID
$ this-& gt; id = uniqid ();
}
function __sleep () {
// do not serverize this- & gt; ( "name"));
    }                                                                                                                                                                                                                                                                                                                                                                        ​= new User;
$u->name = "Leon";
//serialize it Serialization Note that the id attribute is not serialized, the value of id is discarded
$s = serialize($u);
// unserialize it deserialization id is reassigned
$u2 = unserialize($s);
//$u and $u2 have different IDs $u and $u2 have different IDs
print_r($u);
print_r( $u2);
?>

The above has introduced the thirteenth section of the experience of learning from the Third Plenary Session of the 18th CPC Central Committee - Object Serialization, including the content of the experience of learning from the Third Plenary Session of the 18th CPC Central Committee. I hope it will be helpful to friends who are interested in PHP tutorials. .


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template