[ create a new paste ] login | about

Link: http://codepad.org/F6eSp0s0    [ raw code | output | fork ]

php.programmer - PHP, pasted on Apr 14:
<?php
/* sample code to describe the flow of class 
 when we use __sleep() and __wakeup() methods with 
 serilize and unserilize objects.
*/
class a
{
  public $v,$vv,$vvv;
  private function getTime()
  {
    $mtime = microtime();
    $mtime = explode(" ",  $mtime);
    $mtime = (double)($mtime[1]) + (double)($mtime[0]);
    return ($mtime);
  }
  public function __construct()
  {
   echo "\n".'construct '.$this->getTime();
   $this->v = ' v here';
   $this->vv = ' vv here';
   $this->vvv = ' vvv here';
  }
  public function __sleep()
   {
      echo "\n".'sleep '.$this->getTime();
      return array('v','vv'); 
   }
  public function __wakeup()
  {
    echo "\n".'wakeup '.$this->getTime();
    $this->showall();
  }
  public function showall()
  {
   echo "\n*****\n".'showall '.$this->getTime();
   echo "\n".$this->v.'--'.$this->vv.'--'.$this->vvv."\n*****\n";
  }
}
// create object of class
$objA    = new a();

// lets serialize the object
$objASer = serialize($objA);
echo "-\n---serialized data - ".$objASer."\n";

// lets unserialize it
$newObj = unserialize($objASer);

// letc c what happen with the first object
$objA->showall();
?>


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

construct 1239693401.2829
sleep 1239693401.2841-
---serialized data - O:1:"a":2:{s:1:"v";s:7:" v here";s:2:"vv";s:8:" vv here";}

wakeup 1239693401.2848
*****
showall 1239693401.285
 v here-- vv here--
*****

*****
showall 1239693401.2855
 v here-- vv here-- vvv here
*****


Create a new paste based on this one


Comments: