PHP/Class/ wakeup
Версия от 10:37, 26 мая 2010; (обсуждение)
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);
?>
__wakeup() is called by unserialize().
<?
class apple {
var $frozen = 0;
function ___sleep( ) {
$this->frozen++;
return array_keys( get_object_vars( $this) );
}
function __wakeup( ) {
print $this->frozen." time(s)";
}
}
$app = new apple ( );
$stored = serialize( $app );
print $stored;
$new_app = unserialize( $stored );
?>