Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sleep and wakeup magic methods

Posted on 2013-12-07
4
Medium Priority
?
1,120 Views
Last Modified: 2013-12-07
from an object php code tutorial

what is sleep and wakeup doing in this example

Please do not answer with php.net/sleep or php.net/wakeup because I am trying to learn from this example

<?php
class Employee{
  private $fname;
  private $date_of_birth;
  
  public function setFirstName($fname){
    $this->fname=$fname;
  }
  public function getFirstName(){
    return $this->fname;
  }
  public function setBirthDate($dob){
    $this->date_of_birth=$dob;
  }
  public function getBirthDate(){
    return $this->date_of_birth;
  }
  public function __sleep(){
    return array("fname"); //because of this, only name is serialized
  }
  public function __wakeup(){
    if($this->fname == "Marsha"){
      $this->date_of_birth="09-12-1983";
    }
  }
}
$e= new Employee();
$e->setFirstName("Marsha");
$e->setBirthDate("09-12-1983");
$data=serialize($e)."\n";
var_dump (unserialize($data));

Open in new window

0
Comment
Question by:rgb192
  • 2
4 Comments
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 1000 total points
ID: 39703142
When uou call serialize() function, this one looks if your class has a __sleep method and if it does, all code within method is executed before to sereialize the object: as you see in the comment, bacause of this function only  the name is serialized.

__wakeup does the same when you unserialize the object: before to unserialize the __wakeup code is executes. Ususally this code should reconstruct resources as database connection, but in this case simply restore a hardcoded data.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 39703167
See the cautionary tale here:
http://php.net/manual/en/function.unserialize.php#112823

There are many useless things in the world, and in my opinion PHP serialize() is right down there with teats on a boar.  A better and more deliberate strategy is to use JSON_Encode.  Not only do you keep control of the logic flow, but you also get a data string that is compatible with programming outside of PHP (eg: JavaScript).  I would avoid depending on __sleep() and __wakeup() whenever possible.
Other cautionary tales, with unpleasant surprises and a dependency to avoid:
http://php.net/manual/en/language.oop5.magic.php#76643
http://php.net/manual/en/language.oop5.magic.php#98963
0
 

Author Closing Comment

by:rgb192
ID: 39703195
sleep serializes and wakeup unserializes and Ray says this is useless so I will move on to next part of tutorial: Clone.

Thanks.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39703207
Clone is very useful!  It's how you get a copy of an object preserving the state of any changes since instantiation.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question