PHP/Class/ sleep
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;
?>