3v4l.org

run code in 300+ PHP versions simultaneously
<?php header('Content-type: text/plain'); $middleware = ['middle_a', 'middle_b', 'middle_c', 'middle_d', 'middle_e']; function middle_a($output, $next) { echo __FUNCTION__ . ":before\n"; $output = $next($output+1); echo __FUNCTION__ . ":after\n"; return $output; } function middle_b($output, $next) { echo __FUNCTION__ . ":before\n"; $output = $next($output+1); echo __FUNCTION__ . ":after\n"; return $output; } function middle_c($output, $next) { try { echo __FUNCTION__ . ":before\n"; $output = $next($output+1); echo __FUNCTION__ . ":after\n"; return $output; } catch (Exception1 $ex) { echo __FUNCTION__ . ":after\n"; // echo __FUNCTION__ . " caught a `" . get_class($ex) . "` and throws a `Exception2`\n"; // throw new Exception2(-100); return -100; // return $next(-100); } } function middle_d($output, $next) { echo __FUNCTION__ . ":before\n"; $output = $next($output+1); echo __FUNCTION__ . ":after\n"; return $output; } function middle_e($output, $next) { echo __FUNCTION__ . ":before\n"; $output = $next($output+1); echo __FUNCTION__ . ":after\n"; return $output; } class Exception1 extends Exception {} class Exception2 extends Exception {} class Exception3 extends Exception {} function act($input) { echo __FUNCTION__ . ":start\n"; throw new Exception1; return $input; } $pipeline = function($input) { return act($input); }; foreach (array_reverse($middleware) as $cb) { $pipeline = function($input) use ($cb, $pipeline) { return $cb($input, $pipeline); }; } try { var_dump($pipeline(0)); } catch (\Exception $ex) { exit("App caught a `" . get_class($ex) . "`"); }

preferences:
24.57 ms | 406 KiB | 5 Q