Section 13--Object Serialization_PHP Tutorial

WBOY
Release: 2016-07-21 16:01:13
Original
760 people have browsed it

/*
+-------------------------------------------------- ----------------------------------+
| = This article is read by Haohappy<>
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid possible unnecessary trouble, please do not reprint, thank you
| = We welcome criticisms and corrections, and hope to make progress together with all PHP enthusiasts!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+---------- -------------------------------------------------- ------------------+
*/
Section 13 - Object Serialization
Serialization can include variables including objects, Convert to continuous bytes data. You can store the serialized variable in a file or transmit it over the network. Then deserialize it back to the original data. The class you defined before deserializing the object of the class , PHP can successfully store the properties and methods of its objects. Sometimes you may need an object to be executed immediately after deserialization. For such purposes, PHP will automatically look for the __sleep and __wakeup methods.
When an object To be serialized, PHP calls the __sleep method (if one exists). After deserializing an object, PHP calls the __wakeup method. Neither method accepts arguments. The __sleep method must return an array containing Properties that need to be serialized. PHP will discard the values ​​of other properties. If there is no __sleep method, PHP will save all properties.
Example 6.16 shows how to use the __sleep and __wakeup methods to serialize an object. Id The attribute is a temporary attribute that is not intended to be retained in the object. The __sleep method ensures that the id attribute is not included in the serialized object. When deserializing a User object, the __wakeup method establishes the new value of the id attribute. This example is designed 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; > {
//give user a unique ID
$this->id = uniqid();
}
function __sleep()
                                                                                   do not serialize this->id Do not serialize id                                                                                                                                                                 //give user a unique ID
$this->id = uniqid();
} }
} //create object Create an object
$u = new User;
$u-&g t; 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 Rowization id is reassigned
$u2 = unserialize($s);
//$u and $u2 have different IDs $u and $u2 have different IDs
print_r($u);
print_r($u2);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316930.htmlTechArticle/* +--------------------- -------------------------------------------------- --------+ |=This article is Haohappy's notes from the chapter ClassesandObjects when reading CorePHP Programming |=Translation is mainly + personal...
Related labels:
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