Skip to content

Instantly share code, notes, and snippets.

@lyrixx
Created Aug 11, 2017
Embed
What would you like to do?
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