3v4l.org

run code in 300+ PHP versions simultaneously
<?php function decorate(callable $fn, callable $decorator) { $wrapper = $decorator(); return function() use ($fn, $wrapper) { $gen = $wrapper(); // Only call the wrapped function if the decorator made "yield" if (!$gen instanceof \Generator) { return $gen; } // Use the yielded arguments, forward all if none given. if (is_array($gen->current())) { $arguments = $gen->current(); } else { $arguments = func_get_args(); } // Forward all throwed Exceptions try { // Send the decorated function's return value into the // decorator as return value of "yield" $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 "catch_all_exceptions: Caught Exception: $e\n"; } }; } function once() { return function() { static $return; static $called = false; if ($called) { return $return; } else { $return = (yield); $called = true; } }; } function do_something_before() { return function() { echo "Before!\n"; yield; }; } $bar = decorate(function() { echo "Hello\n"; }, "once"); $bar(); $bar(); $foo = decorate(decorate("foo", "catch_all_exceptions"), "do_something_before"); var_dump($foo("bar"));
Output for git.master, git.master_jit, rfc.property-hooks
Hello Hello Before! catch_all_exceptions: Caught Exception: Exception: Error bar! in /in/jo1IG:35 Stack trace: #0 /in/jo1IG(26): foo('bar') #1 /in/jo1IG(26): {closure}('bar') #2 /in/jo1IG(76): {closure}('bar') #3 {main} NULL

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
38.17 ms | 402 KiB | 8 Q