Skip to content

Instantly share code, notes, and snippets.

@rayburgemeestre
Created September 12, 2014 10:34
Show Gist options
  • Save rayburgemeestre/2584bbf9aafd93933d4f to your computer and use it in GitHub Desktop.
Save rayburgemeestre/2584bbf9aafd93933d4f to your computer and use it in GitHub Desktop.
ScopedResourceAllocator
<?php
class ScopedResourceAllocator
{
private $closure = null;
public function __construct(Closure $closure)
{
$this->closure = $closure;
}
public function __destruct()
{
$closure = $this->closure;
$closure();
}
}
function foo()
{
try {
$x = new ScopedResourceAllocator(function () {
print "restoring memory limit to 128\n";
});
print "setting memory limit to 512\n";
throw new \RuntimeException;
}
catch (Exception $ex) {
print "exception\n";
}
}
foo();
/**
* output will be:
*
* setting memory limit to 512
* exception
* restoring memory limit to 128
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment