3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Vagabond\Http; class Request{ private $url; private $method; private $sources = []; public function __construct($url, $method){ $this->url = $url; $this->method = $method; } public function setParameterProvider($name, callable $provider){ $this->sources[$name] = $provider; } public function setParameterSource($name, array $data){ $this->sources[$name] = $data; } public function get($source, $name, $default = null){ if (isset($this->sources[$source]) && !is_array($this->sources[$source])){ $this->sources[$source] = call_user_func($this->sources[$source]); } return (isset($this->sources[$source][$name]))?$this->sources[$source][$name]:$default; } public function getUrl(){ return $this->url; } public function getMethod(){ return $this->method; } public static function getPhpDefaultRequest(){ $request = new Request($_SERVER["REQUEST_URI"], $_SERVER["REQUEST_METHOD"]); $request->setParameterSource("get", $_GET); $request->setParameterSource("post", $_POST); $request->setParameterSource("files", $_FILES); $request->setParameterProvider("session", function(){ return $_SESSION; }); $request->setParameterSource("env", $_ENV); $request->setParameterSource("cookie", $_COOKIE); $request->setParameterSource("server", $_SERVER); } } $request = \Vagabond\Http\Request::getPhpDefaultRequest(); $request->setParameterProvider("post", function(){ if ($_SERVER["CONTENT_TYPE"] == "application/json"){ return json_decode(file_get_contents("php://input"), true); } else { return $_POST; } }); $action = $request->get("get", "action"); $username = $request->get("post", "username"); $password = $request->get("post", "password"); ?>
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.6
Warning: Undefined array key "REQUEST_URI" in /in/jsVnM on line 36 Warning: Undefined array key "REQUEST_METHOD" in /in/jsVnM on line 36 Fatal error: Uncaught Error: Call to a member function setParameterProvider() on null in /in/jsVnM:49 Stack trace: #0 {main} thrown in /in/jsVnM on line 49
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25, 7.4.27 - 7.4.33
Notice: Undefined index: REQUEST_URI in /in/jsVnM on line 36 Notice: Undefined index: REQUEST_METHOD in /in/jsVnM on line 36 Fatal error: Uncaught Error: Call to a member function setParameterProvider() on null in /in/jsVnM:49 Stack trace: #0 {main} thrown in /in/jsVnM on line 49
Process exited with code 255.
Output for 7.3.32 - 7.3.33, 7.4.26
Fatal error: Uncaught Error: Call to a member function setParameterProvider() on null in /in/jsVnM:49 Stack trace: #0 {main} thrown in /in/jsVnM on line 49
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Notice: Undefined index: REQUEST_URI in /in/jsVnM on line 36 Notice: Undefined index: REQUEST_METHOD in /in/jsVnM on line 36 Fatal error: Call to a member function setParameterProvider() on null in /in/jsVnM on line 49
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38
Notice: Undefined index: REQUEST_URI in /in/jsVnM on line 36 Notice: Undefined index: REQUEST_METHOD in /in/jsVnM on line 36 Fatal error: Call to a member function setParameterProvider() on a non-object in /in/jsVnM on line 49
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/jsVnM on line 8
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/jsVnM on line 3
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, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/jsVnM on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/jsVnM on line 3
Process exited with code 255.

preferences:
226.39 ms | 401 KiB | 460 Q