/*
+-------------------------------------------------- ----------------------------------+
| = 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