3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function decorate(callable $fn, callable $decorator) { return function() use ($fn, $decorator) { $d = $decorator(); $gen = $d(); if (is_array($gen->current())) { $arguments = $gen->current(); } else { $arguments = func_get_args(); } // Forward all throwed Exceptions try { $gen->send(call_user_func_array($fn, $arguments)); } catch (\Exception $e) { $gen->throw($e); } }; } function foo($bar) { throw new Exception("Error $bar!"); } function catch_all_exceptions() { return function() { try { (yield); } catch (\Exception $e) { echo "Caught Exception: $e\n"; } }; } $foo = decorate("foo", "catch_all_exceptions"); $foo("bar");
Output for 7.0.0 - 7.1.0
Caught Exception: Exception: Error bar! in /in/6Via6:26 Stack trace: #0 /in/6Via6(17): foo('bar') #1 /in/6Via6(41): {closure}('bar') #2 {main}
Output for hhvm-3.12.0
Caught Exception: exception 'Exception' with message 'Error bar!' in /in/6Via6:26 Stack trace: #0 /in/6Via6(17): foo() #1 /in/6Via6(41): Closure$decorate() #2 {main}
Output for hhvm-3.10.0
Fatal error: Uncaught exception 'Exception' with message 'Need to call next() first' in /in/6Via6:9 Stack trace: #0 /in/6Via6(9): Generator->current() #1 /in/6Via6(41): Closure$decorate() #2 {main}
Process exited with code 255.
Output for 5.5.0 - 5.6.28
Caught Exception: exception 'Exception' with message 'Error bar!' in /in/6Via6:26 Stack trace: #0 [internal function]: foo('bar') #1 /in/6Via6(17): call_user_func_array('foo', Array) #2 /in/6Via6(41): {closure}('bar') #3 {main}
Output for 5.4.0 - 5.4.45
Notice: Use of undefined constant yield - assumed 'yield' in /in/6Via6 on line 33 Fatal error: Call to a member function current() on a non-object in /in/6Via6 on line 9
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Catchable fatal error: Argument 1 passed to decorate() must be an instance of callable, string given, called in /in/6Via6 on line 40 and defined in /in/6Via6 on line 3
Process exited with code 255.
Output for 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/6Via6 on line 5
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/6Via6 on line 5
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting ')' in /in/6Via6 on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting ')' in /in/6Via6 on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/6Via6 on line 3
Process exited with code 255.