PHP in __sleep and __wakeup

Source: Internet
Author: User
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

  • 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.