Created
December 15, 2012 17:14
-
-
Save backbone87/4297299 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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