3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class URI implements \ArrayAccess, \Iterator, \Countable, \JsonSerializable { const SCHEME = 0x01; const USER = 0x02; const PASS = 0x04; const HOST = 0x08; const PORT = 0x10; const PATH = 0x20; const QUERY = 0x40; const FRAGMENT = 0x80; // The private props and magic methods are only implemented like this to give type validation, // these are effectively public properties private $scheme; private $user; private $pass; private $host; private $port; private $path; private $query; private $fragment; // obviously these two properties would be handled internally in a native impl private static $constPropMap = [ self::SCHEME => 'scheme', self::USER => 'user', self::PASS => 'pass', self::HOST => 'host', self::PORT => 'port', self::PATH => 'path', self::QUERY => 'query', self::FRAGMENT => 'fragment', ]; private $iteratorCounter = self::SCHEME; private function validateScheme() { // in the generic URI syntax, only the format of the scheme is rigid return (bool) preg_match('/^[a-z][a-z0-9+.\-]*$/i', $value); } public function __construct($uri) { $parts = []; if (((string) $uri) !== '' && false === $parts = parse_url($uri)) { throw new \InvalidArgumentException('Invalid URI'); } foreach ($parts as $name => $value) { $this->__set($name, urldecode($value)); } } public function __get($name) { if (!in_array($name, self::$constPropMap)) { trigger_error('Undefined property: ' . __CLASS__ . '::$' . $name, E_USER_NOTICE); return null; } return $this->$name; } public function __set($name, $value) { if ($value === null) { $this->$name = null; } else if ($name === 'port') { $this->port = (int) $value; } else if ($name === 'query') { parse_str($value, $this->query); } else if (in_array($name, self::$constPropMap)) { if ($name === 'scheme' && !$this->validateScheme($value)) { throw new \InvalidArgumentException('Invalid URI scheme'); } $this->$name = (string) $value; } else { // because PHP allows expando properties on anything afaik :-( $this->$name = $value; } } public function __toString() { $result = ''; if (isset($this->scheme)) { $result = $this->scheme . ':'; } if (isset($this->host)) { $result .= '//'; if (isset($this->user)) { $result .= urlencode($this->user); if (isset($this->pass)) { $result .= ':' . urlencode($this->pass); } $result .= '@'; } $result .= urlencode($this->host); if (isset($this->port)) { $result .= ':' . $this->port; } } if (isset($this->path)) { $result .= urlencode($this->path); } if (!empty($this->query)) { $result .= '?' . http_build_query($this->query); } if (isset($this->fragment)) { $result .= '#' . urlencode($this->fragment); } return $result; } /* ArrayAccess */ public function offsetExists($name) { return isset($this->$name) || isset(self::$constPropMap[$name]); } public function offsetGet($name) { if (isset(self::$constPropMap[$name])) { return $this->__get(self::$constPropMap[$name]); } else { return $this->__get($name); } } public function offsetSet($name, $value) { if (isset(self::$constPropMap[$name])) { $this->__set(self::$constPropMap[$name], $value); } else { $this->__set($name, $value); } } public function offsetUnset($name) { if (isset(self::$constPropMap[$name])) { $this->__set(self::$constPropMap[$name], null); } else if (in_array($name, self::$constPropMap)) { $this->__set($name, null); } else { unset($this->$name); } } /* Iterator */ public function current() { return $this->{self::$constPropMap[$this->iteratorCounter]}; } public function key() { return self::$constPropMap[$this->iteratorCounter]; } public function next() { $this->iteratorCounter *= 2; } public function rewind() { $this->iteratorCounter = self::SCHEME; } public function valid() { return $this->iteratorCounter <= self::FRAGMENT; } /* Countable */ public function count() { $result = 0; foreach (self::$constPropMap as $const => $name) { if ($this->$name !== null) { $result++; } } return $result; } /* JsonSerializable */ public function jsonSerialize() { return $this->__toString(); } } $uri = new URI('http://www.google.com/'); $uri['query']['foo'] = '&bar'; echo $uri;
Output for 7.0.0 - 7.1.0
Notice: Undefined variable: value in /in/2fBEZ on line 41 Fatal error: Uncaught InvalidArgumentException: Invalid URI scheme in /in/2fBEZ:76 Stack trace: #0 /in/2fBEZ(52): URI->__set('scheme', 'http') #1 /in/2fBEZ(215): URI->__construct('http://www.goog...') #2 {main} thrown in /in/2fBEZ on line 76
Process exited with code 255.
Output for hhvm-3.10.0 - 3.12.0
Notice: Undefined variable: value in /in/2fBEZ on line 41 Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Invalid URI scheme' in /in/2fBEZ:76 Stack trace: #0 /in/2fBEZ(52): URI->__set() #1 /in/2fBEZ(215): URI->__construct() #2 {main}
Process exited with code 255.
Output for 5.4.0 - 5.6.28
Notice: Undefined variable: value in /in/2fBEZ on line 41 Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Invalid URI scheme' in /in/2fBEZ:76 Stack trace: #0 /in/2fBEZ(52): URI->__set('scheme', 'http') #1 /in/2fBEZ(215): URI->__construct('http://www.goog...') #2 {main} thrown in /in/2fBEZ on line 76
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/2fBEZ on line 26
Process exited with code 255.