Php magic method sleep () wakeup ()

Source: Internet
Author: User
Php magic methods sleep () wakeup () _ sleep () and _ wakeup ()

Public array_ Sleep(Void)

Void_ Wakeup(Void)

The serialize () function checks whether a magic method _ sleep () exists in the class (). If this 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 variable names in the object to be serialized. If this method does not return any contentNULLSerialized and generatesE_NOTICELevel error.

Example:

Class user {public $ name; public $ id; function _ construct () {// assign a uniq id $ this-> id = 'asas' to the id member ';} function _ sleep () {// return ('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, id value is re-assigned // The id of the object u and u2 has different values: print_r ($ u); print_r ($ u2 );

Result:

User Object ([name] => Leo [id] => asas) user Object ([name] => Leo [id] => 5621ed9f6614c)

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.