Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sleep and wakeup magic methods

Posted on 2013-12-07
4
Medium Priority
?
1,069 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

719 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