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 of 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
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 ignition id Return (Array ("name"));} Function __wakeup () {// Give User a unique ID $ this-> id = uniqid ();}} // create object creation an object $ u = new user; $ u-> name = "leon"; // serialize IT Serialization Not Serialization ID attribute, the value of ID is abandon $ s = serialize ($ u); // unserialize it Serialization ID is re-assigned $ u2 = unserialize ($ s); // $ u and $ u2 Have Different IDs $ U and $ U2 have different ID Print_R ($ u); print_r ($ u2);?>>>>>>>>>>>>>