Solved

sleep and wakeup magic methods

Posted on 2013-12-07
4
1,029 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 250 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 110

Accepted Solution

by:
Ray Paseur earned 250 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 110

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 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