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; $path = $context->requestPath; 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.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Fatal error: Uncaught TypeError: end(): Argument #1 ($array) must be of type array, string given in /in/YAmBT:61 Stack trace: #0 /in/YAmBT(61): end('/admin/user/') #1 /in/YAmBT(111): context->__construct('/admin/user/') #2 {main} thrown in /in/YAmBT on line 61
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Warning: end() expects parameter 1 to be array, string given in /in/YAmBT on line 61 Warning: count(): Parameter must be an array or an object that implements Countable in /in/YAmBT on line 62 Warning: array_pop() expects parameter 1 to be array, string given in /in/YAmBT on line 63 Warning: array_reverse() expects parameter 1 to be array, string given in /in/YAmBT on line 65 Warning: end() expects parameter 1 to be array, null given in /in/YAmBT on line 66 Warning: array_pop() expects parameter 1 to be array, null given in /in/YAmBT on line 68 hello from root
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33
Warning: end() expects parameter 1 to be array, string given in /in/YAmBT on line 61 Warning: array_pop() expects parameter 1 to be array, string given in /in/YAmBT on line 63 Warning: array_reverse() expects parameter 1 to be array, string given in /in/YAmBT on line 65 Warning: end() expects parameter 1 to be array, null given in /in/YAmBT on line 66 Warning: array_pop() expects parameter 1 to be array, null given in /in/YAmBT on line 68 hello from root

preferences:
233.13 ms | 403 KiB | 326 Q