<?php // Its destructor brings the closure back to life by storing it in a global. class Resurrector { public $closure; public $self; public function __destruct() { $GLOBALS['resurrected'] = $this->closure; } } class Holder { public $castObject; public $resurrector; public $self; } $array = ['victim' => new stdClass(), 'key' => 'value']; $holder = new Holder(); $resurrector = new Resurrector(); // With only string keys, the cast object shares $array's hashtable. $holder->castObject = (object) $array; // The closure holds the same hashtable through its static vars. $resurrector->closure = function () use ($array) { return $array; }; // Self-references make both objects collectable only by the cycle collector. $holder->resurrector = $resurrector; $holder->self = $holder; $resurrector->self = $resurrector; unset($array); gc_collect_cycles(); unset($holder, $resurrector); gc_collect_cycles(); // Frees 'victim', though the resurrected closure still holds it. gc_collect_cycles(); // Reads the freed object. var_dump($resurrected()['victim']);
You have javascript disabled. You will not be able to edit any code.