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)