Solved

sleep and wakeup magic methods

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to close my php post page. 3 42
PHP processing webform 25 45
htaccess add www, redirect to https, keep query string 17 60
Developers / Staff Setup 10 48
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 count occurrences of each item in an array.

710 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