3v4l.org

run code in 300+ PHP versions simultaneously
<?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 git.master, git.master_jit, rfc.property-hooks
Deprecated: Return type of URI::offsetExists($name) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/2fBEZ on line 131 Deprecated: Return type of URI::offsetGet($name) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/2fBEZ on line 136 Deprecated: Return type of URI::offsetSet($name, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/2fBEZ on line 145 Deprecated: Return type of URI::offsetUnset($name) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/2fBEZ on line 154 Deprecated: Return type of URI::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/2fBEZ on line 167 Deprecated: Return type of URI::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/2fBEZ on line 177 Deprecated: Return type of URI::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/2fBEZ on line 172 Deprecated: Return type of URI::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/2fBEZ on line 187 Deprecated: Return type of URI::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/2fBEZ on line 182 Deprecated: Return type of URI::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/2fBEZ on line 194 Deprecated: Return type of URI::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/2fBEZ on line 209 Warning: Undefined variable $value in /in/2fBEZ on line 41 Deprecated: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated 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.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
41.66 ms | 405 KiB | 8 Q