Section 13 - Object Serialization - Classes and Objects in PHP5 [13]
Section 13 - Object Serialized Serialization The variables can include objects, transform into continuous BYTES data. You can save serial variables in a file or transfer it on the network. And then reverse serialization Reduction as the original data. You can successfully store the properties and methods of the object before the object of the anti-serialization class. Sometimes you need an object to be executed immediately after the anti-train. For this Objective, PHP will automatically find __sleep and __wakeup method. When an object is serialized, PHP will call __sleep method (if present). After the object is configured, PHP will call __wakeup method. This Both methods do not accept parameters. __Sleep method must return an array that contains attributes that require serialization. PHP will discard the value of other properties. If there is no __sleep method, PHP will save all properties. Example 6.16 shows how to use _ _Sleep and __wakeup method to serialize an object. ID attribute is a temporary property that is not intended to keep in the object. __sleep method guarantees that there is no ID attribute in the serialized object. When the anti-serializing a User object, __wakeup method establishes a new value for the id attribute. This example is designed to be self-held. In actual development, you may find that objects containing resources (such as images or data streams) require these methods. Listing 6.16 Object Serialization php class user { Public $ name; public $ ID; function __construct () {// Give User a unique ID gives a different ID $ this-> id = uniqid ();} function __sleep () {// do not serialize this-> ID Non-serialization ID Return (Array ("name"));} function __wakeup () {// Give user a unique ID $ this-> id = uniqid ();}} // create object created an object $ u = New user; $ u-> name = "leon"; // serialize IT Serialization Note that the value of the idiochemical ID attribute, the value of the id is abandon $ s = serialize; // unserialize IT anti-serialization ID is re-assigned $ u2 = unserialize ($ s); // $ u and $ u2 hav e Different IDs $ U and $ U2 have different id print_r ($ u); print_r ($ u2);?>