PHP magic method _ sleep _ wakeup (4), __sleep _ wakeup_PHP tutorial

Source: Internet
Author: User
PHP magic method _ sleep _ wakeup (4) ,__ sleep _ wakeup. PHP magic method _ sleep _ wakeup (4), __sleep _ wakeup serialize can serialize variables including objects and convert them into continuous bytes data. you can save the serialized variable into a file. PHP magic method _ sleep _ wakeup (4) ,__ sleep _ wakeup

Serialize can include objects and convert them into continuous bytes data. you can save serialized variables in a file or transmit them over the network. then, the data is deserialized and restored to the original data. PHP can successfully store the attributes and methods of the objects defined before the objects of the deserialization class. sometimes you may need an object to be executed immediately after deserialization. for this purpose, PHP will automatically find the _ sleep and _ wakeup methods.

When an object is serialized, PHP calls the _ sleep method (if any ). after an object is deserialized, PHP calls the _ wakeup method. both methods do not accept parameters. the _ sleep method must return an array containing the attributes to be serialized. PHP will discard other attribute values. if the _ sleep method is not available, PHP will save all attributes.


Before the program is executed, the serialize () function first checks whether there is a magic method _ sleep. if the __sleep () method exists, it is called first before the serialization operation is executed. This function can be used to clear an object and return an array containing all the variable names of the object. If this method does not return any content, NULL is serialized, resulting in an E_NOTICE error. In contrast, unserialize () checks whether a _ wakeup method exists. If yes, the _ wakeup method is called to prepare the object data in advance.

The _ sleep method is often used to submit uncommitted data or perform similar operations. At the same time, if you have some large objects that do not need to be saved, this function is very useful. _ Wakeup is 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 attribute not intended to be retained in the object. the _ sleep method ensures that the id attribute is not included in the serialized object. when a User object is deserialized, the __wakeup method creates a new value for the id attribute. this example is designed to be self-sustaining. in actual development, you may find that objects containing resources (such as or data streams) need these methods.

 
 Id = uniqid ();} function _ sleep () {// The return (array ('name') of the id member is not serialized here;} function _ wakeup () {$ this-> id = uniqid () ;}}$ u = new user (); $ u-> name = "Leo"; $ s = serialize ($ u ); // serialize serialized object u. The id attribute is not serialized here, and the id value is discarded $ u2 = unserialize ($ s); // unserialize deserialization, the id value is re-assigned. // The id value of the object u and u2 is print_r ($ u); print_r ($ u2);?>

Example 3 :__ A defect of the wakeup method requires attention. if you want to unserialize an object, you

 
 Name) ;}}$ a = new A (); $ a-> name = "foo"; $ a-> B = new B (); // We expect the output here: foo, but the actual output is NULL after the code is executed. $ a-> B-> parent = $ a; $ s = serialize ($ a); $ a = unserialize ($ s);?>

Cause: $ B object unserialized before $ name. Therefore, when B ::__ wakeup is executed, $ a-> name has not been assigned a value.

Therefore, be careful when defining the execution sequence of variables in the class.

Address: http://blog.sina.com.cn/s/blog_758ddcb90100yk05.html

Http://www.bkjia.com/PHPjc/917484.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/917484.htmlTechArticlePHP magic method _ sleep _ wakeup (4), __sleep _ wakeup serialize can be used to serialize variables including objects and convert them into continuous bytes data. you can save the serialized variable to a file...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.