3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Router { protected $routes = []; protected $params = [] ; protected $namespace = 'App\Controllers\\'; public function add($route , $params ) { $route = preg_replace('/^\//','' , $route); $route = preg_replace('/\//' , '\\/' , $route); $route = preg_replace('/\{([a-z]+)\}/' , '?([a-z0-9ا-ی]+)' , $route); $route = '/^' . $route . '\/?$/i'; if(is_string($params)) { list($AllParams['controller'] , $AllParams['method']) = explode('@' , $params); } if(is_array($params)) { list($AllParams['controller'] , $AllParams['method']) = explode('@' , $params['uses']); unset($params['uses']); $AllParams = array_merge($AllParams , $params); } $this->routes[$route] = $AllParams; } public function match($url) { foreach ($this->routes as $route => $params) { if(preg_match($route , $url , $matches)) { foreach ($matches as $key => $match) { if(is_string($key)) { $params['params'][$key] = $match; } } $this->params = $params; return true; } } return false; } public function dispatch($url) { $url = $this->removeVariblesOfQueryString($url); if($this->match($url)) { $controller = $this->params['controller']; $controller = $this->getNameSpace() . $controller; if(class_exists($controller)) { $controller_object = new $controller(); $method = $this->params['method']; if(is_callable([$controller_object , $method])) { $this->params['params'] = isset($this->params['params']) ? $this->params['params'] : []; echo call_user_func_array([$controller_object , $method] , $this->params['params']); } else { throw new \Exception("Method {$method} (in controller {$controller}) not found"); } } else { throw new \Exception("Controller class {$controller} not found",404); } } else { throw new \Exception("no route matched.",404); } } public function getRoutes() { return $this->routes; } public function getParams() { return $this->params; } protected function getNameSpace() { $namespace = $this->namespace; if(array_key_exists('namespace' , $this->params)) { $namespace .= $this->params['namespace'] . '\\'; } return $namespace; } protected function removeVariblesOfQueryString($url) { if($url != '') { $parts = explode("&" , $url , 2); if(strpos($parts[0] , '=') === false) { $url = $parts[0]; } else { $url = ''; } return $url; } } } $router = new Router(); $router->add('/news/{city}','cityController@index'); $router->dispatch('news/اراک');
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 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 Exception: Controller class App\Controllers\cityController not found in /in/5V1Ne:72 Stack trace: #0 /in/5V1Ne(116): Router->dispatch('news/\xD8\xA7\xD8\xB1\xD8\xA7\xDA\xA9') #1 {main} thrown in /in/5V1Ne on line 72
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 Exception: Controller class App\Controllers\cityController not found in /in/5V1Ne:72 Stack trace: #0 /in/5V1Ne(116): Router->dispatch('news/\xD8\xA7\xD8\xB1\xD8\xA7\xDA\xA9') #1 {main} thrown in /in/5V1Ne on line 72
Process exited with code 255.
Output for 5.4.27 - 5.4.45, 5.5.11 - 5.5.38, 5.6.0 - 5.6.40
Fatal error: Uncaught exception 'Exception' with message 'Controller class App\Controllers\cityController not found' in /in/5V1Ne:72 Stack trace: #0 /in/5V1Ne(116): Router->dispatch('news/\xD8\xA7\xD8\xB1\xD8\xA7\xDA\xA9') #1 {main} thrown in /in/5V1Ne on line 72
Process exited with code 255.
Output for 5.4.0 - 5.4.26, 5.5.0 - 5.5.10
Fatal error: Uncaught exception 'Exception' with message 'Controller class App\Controllers\cityController not found' in /in/5V1Ne:72 Stack trace: #0 /in/5V1Ne(116): Router->dispatch('news/????????') #1 {main} thrown in /in/5V1Ne on line 72
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/5V1Ne on line 5
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/5V1Ne on line 5
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/5V1Ne on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/5V1Ne on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/5V1Ne on line 5
Process exited with code 255.

preferences:
313.02 ms | 401 KiB | 463 Q