Controlling serialization using __sleep() and __wakeUp() : __sleep « Class « PHP






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();
    }
}
?>
  
  








Related examples in the same category

1.__sleep() method is called by serialize() before it packs up the object.