Skip to content

Instantly share code, notes, and snippets.

@lyrixx
Created August 11, 2017 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyrixx/b7892ade46724a43cc798a8e7b2267d6 to your computer and use it in GitHub Desktop.
Save lyrixx/b7892ade46724a43cc798a8e7b2267d6 to your computer and use it in GitHub Desktop.
Box PHP calls
<?php
class Foobar
{
public function success()
{
return $this->box('doSuccess', false, '123', '456');
}
public function fail()
{
return $this->box('doFail', false, '789', '101112');
}
// Sandbox all method calls
private function box($method, $defaultReturnValue = null, ...$args)
{
set_error_handler(__CLASS__.'::handleInternalError');
try {
return [$this, $method](...$args);
} catch (\ErrorException $e) {
// FIXME: proper logging here
error_log("OUPPPPPPPPS, exception detected:\n".(string) $e);
} finally {
restore_error_handler();
}
return $defaultReturnValue;
}
private function doSuccess($a = null, $b = null)
{
dump(['succes params' => [$a, $b]]);
return true;
}
private function doFail($a = null, $b = null)
{
dump(['fail params' => [$a, $b]]);
// Will trigger a notice...
$c = $d + 1;
// will not be executed because of the notice
return true;
}
private static function handleInternalError($type, $message, $file, $line)
{
throw new \ErrorException($message, 0, $type, $file, $line);
}
}
$foobar = new Foobar();
dump(['success return' => $foobar->success()]);
dump(['fail return' => $foobar->fail()]);
$c = $d +1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment