In PHP, __sleep and ___wakeup are two separate methods that are called before and after the object is serialized,
Where __sleep is called before an object is serialized, it does not receive any arguments, but returns an array where the properties that need to be serialized can be placed, such as the following example:
Java code
Class Customer {private $name; private $credit _card_number; public function SetName ($name) {$this->name = $name;} pu Blic function GetName () {return $this->name;} public Function SETCC ($CC) {$this->credit_card_number = $cc;} Publ IC function GETCC () {return $this->credit_card_number;} public Function __sleep () {return Array ("name");//Only name sequence {}} $c = new Customer (); $c->setname ("Stuard"); $c->SETCC ("456789″"); $data = Serialize ($c). " \ n "; echo $data. " \ n "; Output:o:8: "Customer": 1:{s:14: "Customer Name"; s:5: "Stuard";}
Before serialization, __sleep specifies that only the Name property is serialized, and Creaditcard does not.
__wakeup, on the other hand, is triggered before deserialization, such as the following example:
Java code
Class Customer {private $name; private $credit _card_number; public function SetName ($name) {$this->name = $name;} pu Blic function GetName () {return $this->name;} public Function SETCC ($CC) {$this->credit_card_number = $cc;} Publ IC function GETCC () {return $this->credit_card_number;} public Function __sleep () {return Array ("name"), Public fu Nction __wakeup () {if ($this->name = = "Stuart") {//re-obtains $this in the database->credit_card_number = "1234567890123456″;}}} $c = new Customer (); $c->setname ("Stuart"); $c->SETCC ("1234567890123456″"); $data = Serialize ($c). " \ n "; Var_dump (Unserialize ($data)); Output:object (Customer) #2 (2) {["Name:private"?] = = String (5) "Stuart"? ["? Credit_card_number:private"]=> string (16) ' 1234567890123456³}
In the above code, because __sleep is used in serialization, the Creadit Cardnumber property is serialized, so the __wakeup method is called before deserialization of the unserialize call, such as the ability to re-obtain the data in the database before doing so