3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function decorate(callable $fn, callable $decorator) { $wrapper = $decorator($fn); return function() use ($wrapper) { return call_user_func_array($wrapper, func_get_args()); }; } function foo($bar) { throw new Exception("Error $bar!"); } function catch_all_exceptions(callable $fn) { return function() use ($fn) { try { return call_user_func_array($fn, func_get_args()); } catch (\Exception $e) { echo "catch_all_exceptions: Caught Exception: $e\n"; } }; } function once(callable $fn) { return function() use ($fn) { static $return; static $called = false; if (!$called) { $return = call_user_func_array($fn, func_get_args()); $called = true; } return $return; }; } function do_something_before(callable $fn) { return function() use ($fn) { echo "Before!\n"; return call_user_func_array($fn, func_get_args()); }; } $bar = decorate(function() { echo "Hello\n"; }, "once"); $bar(); $bar(); $bar(); $foo = decorate(decorate("foo", "catch_all_exceptions"), "do_something_before"); var_dump($foo("bar"));
Output for 7.0.0 - 7.1.0
Hello Before! catch_all_exceptions: Caught Exception: Exception: Error bar! in /in/lrk7A:14 Stack trace: #0 /in/lrk7A(21): foo('bar') #1 /in/lrk7A(8): {closure}('bar') #2 /in/lrk7A(46): {closure}('bar') #3 /in/lrk7A(8): {closure}('bar') #4 /in/lrk7A(56): {closure}('bar') #5 {main} NULL
Output for hhvm-3.10.0 - 3.12.0
Hello Before! catch_all_exceptions: Caught Exception: exception 'Exception' with message 'Error bar!' in /in/lrk7A:14 Stack trace: #0 /in/lrk7A(21): foo() #1 /in/lrk7A(8): Closure$catch_all_exceptions() #2 /in/lrk7A(46): Closure$decorate() #3 /in/lrk7A(8): Closure$do_something_before() #4 /in/lrk7A(56): Closure$decorate() #5 {main} NULL
Output for 5.4.0 - 5.6.28
Hello Before! catch_all_exceptions: Caught Exception: exception 'Exception' with message 'Error bar!' in /in/lrk7A:14 Stack trace: #0 [internal function]: foo('bar') #1 /in/lrk7A(21): call_user_func_array('foo', Array) #2 [internal function]: {closure}('bar') #3 /in/lrk7A(8): call_user_func_array(Object(Closure), Array) #4 [internal function]: {closure}('bar') #5 /in/lrk7A(46): call_user_func_array(Object(Closure), Array) #6 [internal function]: {closure}('bar') #7 /in/lrk7A(8): call_user_func_array(Object(Closure), Array) #8 /in/lrk7A(56): {closure}('bar') #9 {main} NULL
Output for 5.3.0 - 5.3.29
Catchable fatal error: Argument 1 passed to decorate() must be an instance of callable, instance of Closure given, called in /in/lrk7A on line 50 and defined in /in/lrk7A 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/lrk7A on line 7
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/lrk7A on line 7
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting ')' in /in/lrk7A 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/lrk7A on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/lrk7A on line 3
Process exited with code 255.