3v4l.org

run code in 300+ PHP versions simultaneously
<?php class VerboseException extends RuntimeException { protected $errv = []; public function getVerbose(): array /* {{{ */ { return $this->errv; } /* }}} */ final public static function harvest(string $msg, callable $func): void /* {{{ */ { $e = new static ($msg); $func((function ($err): void { $this->errv[] = $err; })->bindTo($e)); if (!empty($e->errv)) throw $e; } /* }}} */ } /* example usage */ try { VerboseException::harvest("test", function (callable $error): void { $error("foo"); $error("bar"); $error("baz"); }); } catch (VerboseException $e) { echo "Error: {$e->getMessage()}\n"; print_r($e->getVerbose()); }
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4, 8.3.6
Error: test Array ( [0] => foo [1] => bar [2] => baz )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Error: test Array ( [0] => foo [1] => bar [2] => baz )
Output for 7.0.0 - 7.0.33
Fatal error: Uncaught TypeError: Return value of VerboseException::{closure}() must be an instance of void, none returned in /in/tjD2t:19 Stack trace: #0 /in/tjD2t(35): VerboseException->{closure}('foo') #1 /in/tjD2t(19): {closure}(Object(Closure)) #2 /in/tjD2t(38): VerboseException::harvest('test', Object(Closure)) #3 {main} thrown in /in/tjD2t on line 19
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/tjD2t on line 7
Process exited with code 255.

preferences:
196.69 ms | 401 KiB | 289 Q