3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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; }
based on hOP77
Output for 7.2.0
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.24 - 7.1.10
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
Output for 5.4.10 - 5.4.45
Parse error: syntax error, unexpected 'end' (T_STRING) in /in/YAmBT on line 5
Process exited with code 255.
Output for 5.3.20 - 5.3.29
Parse error: syntax error, unexpected T_STRING in /in/YAmBT on line 5
Process exited with code 255.