Solved

sleep and wakeup magic methods

Posted on 2013-12-07
4
905 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 30

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 108

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 108

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now