Using __sleep() and __wakeup() for Objects : __wakeup « Class « PHP






Using __sleep() and __wakeup() for Objects

 
<?php
     class UserClass {
          public $sessionID;
          public $username;

          public function __sleep() {
               session_destroy();
               return array("username");
          }
          public function __wakeup() {
               session_start();
               $this->sessionId = session_id();
          }
     }
     session_start();
     $user = new UserClass;
     $user->sessionId = session_id();
     $seralized_user = serialize($user);

     unset($user);
     $user = unserialize($serialized_user);
?>
  
  








Related examples in the same category

1.__wakeup() is called by unserialize().