3v4l.org

run code in 300+ PHP versions simultaneously
<?php class objectDispatch { public function routeIterator(&$path) { while (!empty($path)) { yield end($path); array_pop($path); } } public function __invoke($context, $root) { $last = ''; $parent = null; $current = $root; $isEndpoint = false; foreach($this->routeIterator($path) as $chunk) { if (!is_object($current) && class_exists($current)) { $current = new $current(); } if (is_object($current)) { $parent = $current; } if (in_array($chunk, get_class_methods($parent))) { yield $parent->$chunk($path); $isEndpoint = true; break; } else if (array_key_exists($chunk, get_object_vars($parent))) { $current = $parent->$chunk; } else if (method_exists($parent, 'lookup')) { list($current, $consumed) = $parent->lookup($path); $chunk = implode('/', $consumed); $truncateAt = count($consumed); array_splice($path, $truncateAt, ($truncateAt - count($path))); } else { header('Http/1.0 404 Not Found'); exit(); } $last = $chunk; } //No parts remaining if (!is_object($current) && class_exists($current)) { $current = new $current(); } if (!$isEndpoint) { if (is_object($current)) { yield $current(); } else if (is_object($parent)) { yield $parent(); } } } } class context { public $requestPath; public $trailing; public function __construct($path) { $this->trailing = false; $requestPath = explode('/', $path); if (end($path) == '') { $this->trailing = (count($path) > 2); array_pop($path); } $path = array_reverse($path); if (end($path) == '') { //Eliminate leading slashes array_pop($path); } $this->requestPath = $path; } } class rootController { public $admin = 'adminController'; public function __invoke($args = []) { return "hello from root"; } } class adminController { public $user = 'usersController'; public function __invoke($args = []) { return "hello from admin"; } } class usersController { public function __invoke($args = []) { return "hello from users"; } public function lookup($path) { $current = new userController($path[0]); return [$current, [$path[0]]]; } } class userController { public $id; public function __construct($id) { $this->id = $id; } public function __invoke($args = []) { return "hello from user " . $this->id; } } $context = new context('/admin/user/'); $dispatch = new objectDispatch(); foreach ($dispatch($context, 'rootController') as $dispatchMessage) { echo $dispatchMessage; }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Fatal error: Uncaught TypeError: end(): Argument #1 ($array) must be of type array, string given in /in/hOP77:59 Stack trace: #0 /in/hOP77(59): end('/admin/user/') #1 /in/hOP77(109): context->__construct('/admin/user/') #2 {main} thrown in /in/hOP77 on line 59
Process exited with code 255.
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 Fatal error: Uncaught TypeError: end(): Argument #1 ($array) must be of type array, string given in /in/hOP77:59 Stack trace: #0 /in/hOP77(59): end('/admin/user/') #1 /in/hOP77(109): context->__construct('/admin/user/') #2 {main} thrown in /in/hOP77 on line 59
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33
Warning: end() expects parameter 1 to be array, string given in /in/hOP77 on line 59 Warning: count(): Parameter must be an array or an object that implements Countable in /in/hOP77 on line 60 Warning: array_pop() expects parameter 1 to be array, string given in /in/hOP77 on line 61 Warning: array_reverse() expects parameter 1 to be array, string given in /in/hOP77 on line 63 Warning: end() expects parameter 1 to be array, null given in /in/hOP77 on line 64 Warning: array_pop() expects parameter 1 to be array, null given in /in/hOP77 on line 66 hello from root
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20
Warning: end() expects parameter 1 to be array, string given in /in/hOP77 on line 59 Warning: array_pop() expects parameter 1 to be array, string given in /in/hOP77 on line 61 Warning: array_reverse() expects parameter 1 to be array, string given in /in/hOP77 on line 63 Warning: end() expects parameter 1 to be array, null given in /in/hOP77 on line 64 Warning: array_pop() expects parameter 1 to be array, null given in /in/hOP77 on line 66 hello from root

preferences:
176.13 ms | 403 KiB | 183 Q