Skip to content

Instantly share code, notes, and snippets.

@mikey179
Last active December 11, 2015 00:38
Show Gist options
  • Save mikey179/4517598 to your computer and use it in GitHub Desktop.
Save mikey179/4517598 to your computer and use it in GitHub Desktop.
with experiment
<?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