3v4l.org

run code in 300+ PHP versions simultaneously
<?php //Enter your code here, enjoy! $array = [ 'database' => [ 'host' => 'localhost', 'db_name' => 'test', 'user' => 'root', 'password' => '' ], 'locale' => [ 'timezone' => 'Europe/Rome', 'default_language' => 'en' ], 'mailer' => [ 'system' => 'phpmail' ], 'session' => [ 'id' => 'vela_id' ] ]; /** * Config class * * Simple class to store or get elements from configuration registry */ class Config implements \ArrayAccess, \IteratorAggregate { /** @var array $data Data configuration array */ private $data = []; /** * Class constructor * @param array $data List of values to add to the configuration registry */ public function __construct(array $data = []) { $ritit = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($data)); $result = array(); foreach ($ritit as $leafValue) { $keys = array(); foreach (range(0, $ritit->getDepth()) as $depth) { $keys[] = $ritit->getSubIterator($depth)->key(); } $result[implode('.', $keys)] = $leafValue; } $this->data = $result; } /** * Retrieves elements from config array * * @param string $key * @return mixed returns a config value * @throws Exception when no $key found */ public function get($key) { if (!isset($this->data[$key])) { throw new \Exception('There is no entry for key: ' . $key); } return $this->data[$key]; } /** * Return true if value is empty for given key * * @param string $key * @return bool */ public function isEmpty($key) { return empty($this->data[$key]); } /** * IteratorAggregate interface required method * * @return \ArrayIterator */ public function getIterator() { return new \ArrayIterator($this->data); } /** * Key to set * * @param mixed $key * @param mixed $value * @throws \Exception */ public function offsetSet($key, $value) { if (!$key) { $this->data[] = $value; } else { $this->data[$key] = $value; } } /** * Key to retrieve * * @param mixed $key * @return string|null */ public function offsetGet($key) { if (isset($this->data[$key])) { return $this->data[$key]; } return null; } /** * Whether a key exists * * @param mixed $key * @return bool */ public function offsetExists($key) { return isset($this->data[$key]); } /** * Key to unset * * @param mixed $key */ public function offsetUnset($key) { unset($this->data[$key]); } } $config = new Config($array); var_dump($config->get('database'));
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Return type of Config::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/FDjG9 on line 141 Deprecated: Return type of Config::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/FDjG9 on line 126 Deprecated: Return type of Config::offsetSet($key, $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/FDjG9 on line 109 Deprecated: Return type of Config::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/FDjG9 on line 151 Deprecated: Return type of Config::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/FDjG9 on line 97 Fatal error: Uncaught Exception: There is no entry for key: database in /in/FDjG9:75 Stack trace: #0 /in/FDjG9(160): Config->get('database') #1 {main} thrown in /in/FDjG9 on line 75
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:
54.13 ms | 402 KiB | 8 Q