PHP/Class/ sleep — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 10:37, 26 мая 2010
Controlling serialization using __sleep() and __wakeUp()
<?php
class LogFile {
protected $filename;
protected $handle;
public function __construct($filename) {
$this->filename = $filename;
$this->open();
}
private function open() {
$this->handle = fopen($this->filename, "a");
}
public function __destruct($filename) {
fclose($this->handle);
}
public function __sleep() {
return array("filename");
}
public function __wakeUp() {
$this->open();
}
}
?>
__sleep() method is called by serialize() before it packs up the object.
<?
class apple {
var $flavor="sweet";
var $frozen = 0;
function ___sleep( ) {
$this->frozen++;
return array_keys( get_object_vars( $this) );
}
}
$app = new apple ( );
$stored = serialize( $app );
print $stored;
?>