3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Config implements ArrayAccess, Countable { /** * @var ArrayObject */ private $storage; /** * @param array $preset */ public function __construct($preset) { $this->storage = new ArrayObject($preset); $this->storage->setFlags( ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS ); } /** * ArrayAccess offsetGet (getter). * * @param string $index */ public function offsetGet($index) { return isset($this->storage->{$index}) ? $this->storage->{$index} : null; } /** * ArrayAccess offsetSet (setter). * * @param string $index * @param mixed $value */ public function offsetSet($index, $value) { if (is_null($index)) { $this->storage[] = $value; } else { $this->storage->{$index} = $value; } } /** * ArrayAccess offsetExists (isset). * * @param string $index */ public function offsetExists($index) { return isset($this->storage->{$index}); } /** * ArrayAccess offsetUnset (unset). * * @param string $index */ public function offsetUnset($index) { unset($this->storage->{$index}); } /** * Magic method (getter). * * @param string $index */ public function __get($index) { return $this->offsetGet($index); } /** * Magic method (setter). * * @param string $index * @param mixed $value */ public function __set($index, $value) { return $this->offsetSet($index, $value); } /** * Magic method (isset). * * @param string $index */ public function __isset($index) { return $this->offsetExists($index); } /** * Magic method (unset). * * @param string $index */ public function __unset($index) { return $this->offsetUnset($index); } /** * Magic method (as function invoker). * * @param mixed $arguments */ public function __invoke(...$arguments) { if (isset($this->storage->{$arguments[0]})) { return $this->storage->{$arguments[0]}; } } /** * Magic method (toString well json). */ public function __toString() { $return = []; foreach ($this->storage as $key => $value) { $return[$key] = $value; } return json_encode($return, JSON_PRETTY_PRINT); } /** * Magic method (override print_r/var_dump). */ public function __debugInfo() { $return = []; foreach ($this->storage as $key => $value) { $return[$key] = $value; } return $return; } /** * Implements Countable */ public function count() { return $this->storage->count(); } } $config = new Config([ 'url' => 'https://example.com', 'title' => 'My dynamic website' ]); echo $config->url.PHP_EOL; echo $config['title'].PHP_EOL; echo count($config).PHP_EOL; echo print_r($config, true).PHP_EOL; echo $config.PHP_EOL;
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Deprecated: Return type of Config::offsetExists($index) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cfd4t on line 50 Deprecated: Return type of Config::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cfd4t on line 25 Deprecated: Return type of Config::offsetSet($index, $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/Cfd4t on line 36 Deprecated: Return type of Config::offsetUnset($index) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cfd4t on line 60 Deprecated: Return type of Config::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cfd4t on line 144 https://example.com My dynamic website 2 Config Object ( [url] => https://example.com [title] => My dynamic website ) { "url": "https:\/\/example.com", "title": "My dynamic website" }
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
https://example.com My dynamic website 2 Config Object ( [url] => https://example.com [title] => My dynamic website ) { "url": "https:\/\/example.com", "title": "My dynamic website" }

preferences:
171.36 ms | 410 KiB | 5 Q