Skip to content

Instantly share code, notes, and snippets.

@KAYLukas
Created May 11, 2014 17:28
Show Gist options
  • Save KAYLukas/0b1d65e57b52862f8da5 to your computer and use it in GitHub Desktop.
Save KAYLukas/0b1d65e57b52862f8da5 to your computer and use it in GitHub Desktop.
class Mock{
private $context;
public function __construct($a){
$this->context = new Context($a);
}
public function __destruct(){
Context::destroyContext($this->context);
}
}
class Context {
public $a;
public static $ref = [];
public function __construct($a){
$this->a = $a;
array_push(self::$ref, $this);
}
public function destroyContext($context){
self::$ref = array_filter(self::$ref, function($ref) use ($context){ return $context !== $ref;});
}
public static function getCurrentContext(){
return end(self::$ref);
}
}
function x($i){
$a = new Mock($i);
var_dump(Context::getCurrentContext());
if($i < 100){
x($i + 1);
}
var_dump(Context::getCurrentContext());
}
x(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment