Created
September 12, 2014 10:34
-
-
Save rayburgemeestre/2584bbf9aafd93933d4f to your computer and use it in GitHub Desktop.
ScopedResourceAllocator
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 | |
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