Created
August 11, 2017 16:14
-
-
Save lyrixx/b7892ade46724a43cc798a8e7b2267d6 to your computer and use it in GitHub Desktop.
Box PHP calls
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 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