PHP Magic Method __sleep __wakeup (iv), __sleep__wakeup
Serialization serialize can convert variables including objects into continuous bytes data. You can either have a serialized variable in a file or transfer it over the network. Then crossdress the rows to revert to the original data. The classes that you define before you crossdress the objects 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 the crossdress is serialized. For this purpose, PHP will automatically look for __sleep and __wakeup methods.
When an object is serialized, PHP invokes the __sleep method (if one exists). After crossdress an object, PHP calls the __wakeup method. Both methods do not accept parameters. The __sleep method must return an array containing the properties that need to be serialized. PHP discards the values of other properties. If there is no __sleep method, PHP will save all properties.
Before the program executes, the Serialize () function first checks for the existence of a magic method __sleep. If present, the __sleep () method is called first before performing a serialization (serialization) operation. This feature can be used to clean up an object and return an array that contains all the variable names in the object. If the method does not return anything, then NULL is serialized, resulting in a e_notice error. In contrast, unserialize () checks for the existence of a __wakeup method. If present, the __wakeup method is called before the object data is prepared beforehand.
The __sleep method is often used to commit uncommitted data, or similar operations. At the same time, if you have some very large objects, do not need to save, this function is very useful. __wakeup are often used in deserialization operations, such as re-establishing a database connection, or performing other initialization operations.
Server = $server; $this->username = $username; $this->password = $password; $this->db = $db; $this->connect (); } Private function Connect () { $this->link = mysql_connect ($this->server, $this->username, $this- password); mysql_select_db ($this->db, $this->link); } Public Function __sleep () { return array (' Server ', ' username ', ' password ', ' db '); } Public Function __wakeup () { $this->connect (); }}? >
The following example shows how to serialize an object using the __sleep and __wakeup methods. The id attribute is a temporary property that is not intended to be persisted in the object. The __sleep method guarantees that the ID attribute is not included in the serialized object. When crossdress a user object, the __wakeup method establishes the new value of the id attribute. This example is designed to be self-sustaining. In real-world development, you might find that objects that contain resources (like or data streams) require these methods.
id = uniqid (); } function __sleep () { //here does not serialize ID member return (Array (' name ')); } function __wakeup () { $this->id = Uniqid (); } } $u = new User (); $u->name = "Leo"; $s = serialize ($u); Serialize serialization Object U, where the id attribute is not serialized, the ID value is discarded $u2 = unserialize ($s); Unserialize crossdress, id value is re-assigned//Object U and U2 have different ID assignment print_r ($u);p Rint_r ($u 2);? >
Example three:a flaw in the __wakeup method requires attention, if you intend to unserialize an object, you
name);
Cause: The $b object was unserialized before $name. So when the b::__wakeup executes, the $a->name is not yet assigned
So be careful you define the order in which variables in the class are executed.
Original address: http://blog.sina.com.cn/s/blog_758ddcb90100yk05.html
http://www.bkjia.com/PHPjc/917484.html www.bkjia.com true http://www.bkjia.com/PHPjc/917484.html techarticle PHP Magic Method __sleep __wakeup (iv), __SLEEP__WAKEUP serialization serialize can convert variables including objects into continuous bytes data. You can put the serialized variable in a file ...