Skip to content

Instantly share code, notes, and snippets.

@backbone87
Created December 15, 2012 17:14
Show Gist options
  • Save backbone87/4297299 to your computer and use it in GitHub Desktop.
Save backbone87/4297299 to your computer and use it in GitHub Desktop.
<?php
$cache = array();
$insert = function(stdClass $stat) use(&$cache) {
echo 'insert '; var_dump($stat->i);
$cache[spl_object_hash($stat)] = $stat;
};
$delete = function(stdClass $stat) use(&$cache) {
echo 'delete '; var_dump($stat->i);
unset($cache[spl_object_hash($stat)]);
};
class Test {
private static $n = 0;
private $stat;
private $delete;
public function __construct($insert, $delete) {
$this->delete = $delete;
$this->stat = new stdClass();
$this->stat->i = self::$n++;
echo 'construct '; $this->stat();
$insert($this->stat);
}
public function __destruct() {
echo 'destruct '; $this->stat();
$delete = $this->delete;
$delete($this->stat);
}
public function stat() {
var_dump($this->stat->i);
}
}
echo '<pre>';
$a = new Test($insert, $delete);
$b = new Test($insert, $delete);
unset($a);
echo 'script end' . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment