Last active
December 11, 2015 00:38
-
-
Save mikey179/4517598 to your computer and use it in GitHub Desktop.
with experiment
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 | |
function with($var, \Closure $execute, \Closure $exit = null) | |
{ | |
try { | |
$result = $execute($var); | |
$exit($var); | |
} catch (\Exception $e) { | |
if (null == $exit) { | |
return null; | |
} | |
$result = $exit($var, $e); | |
} | |
return $result; | |
} | |
// example: read from a file, ignoring any exceptions | |
with($f = new FileInputStream('foo.txt'), | |
function(FileInputStream $f) | |
{ | |
return $f->read(); | |
} | |
); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment