3v4l.org

run code in 300+ PHP versions simultaneously
<?php // modified from: http://us1.php.net/manual/en/class.arrayaccess.php#99236 // sanity and error checking omitted for brevity // note: it's a good idea to implement arrayaccess + countable + // an iterator interface (like iteratoraggregate) as a triplet /* // WORKS. class Template implements ArrayAccess { // makes it so we can do $template_inst['foo']; public $config = array(); // necessary for deep copies public function __clone() { foreach ($this->config as $key => $value) if ($value instanceof self) $this[$key] = clone $value; } public function __construct(array $config = array()) { foreach ($config as $key => $value) $this[$key] = $value; } public function offsetSet($offset, $config) { if (is_array($config)) { $config = new self($config); } if ($offset === null) { // don't forget this! $this->config[] = $config; } else { $this->config[$offset] = $config; } } public function toArray() { $config = $this->config; foreach ($config as $key => $value) if ($value instanceof self) $config[$key] = $value->toArray(); return $config; } // as normal public function offsetGet($offset) { return $this->config[$offset]; } public function offsetExists($offset) { return isset($this->config[$offset]); } public function offsetUnset($offset) { unset($this->config); } } */ // DOESN'T WORK class Template implements ArrayAccess { // makes it so we can do $template_inst['foo']; // container for items passed with null $offset: $this[] = $value; public $implied_key_list = array(); // necessary for deep copies public function __clone() { foreach ($this as $key => $value) if ($value instanceof self) $this[$key] = clone $value; } public function __construct(array $items = array()) { foreach ($items as $key => $value) { $this[$key] = $value; } } public function offsetSet($offset, $value) { // if (is_array($value)) { $value = new self(array($offset => $value)); } # segfault from this line if ($offset === null) { // don't forget this! allows `$this[] = $value;` $this->implied_key_list[] = $value; } else { $this->$offset = $value; } } // as normal public function offsetGet($offset) { return $this->$offset; } public function offsetExists($offset) { return isset($this->$offset); } public function offsetUnset($offset) { unset($this->$offset); } } $test = new Template(array('noises' => array('dog' => 'woof'))); echo "<pre><code>"; print_r($test); echo "</code></pre>";
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
Deprecated: Return type of Template::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/eQN3R on line 77 Deprecated: Return type of Template::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/eQN3R on line 76 Deprecated: Return type of Template::offsetSet($offset, $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/eQN3R on line 66 Deprecated: Return type of Template::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/eQN3R on line 78 Deprecated: Creation of dynamic property Template::$noises is deprecated in /in/eQN3R on line 71 <pre><code>Template Object ( [implied_key_list] => Array ( ) [noises] => Array ( [dog] => woof ) ) </code></pre>
Output for 8.1.0 - 8.1.28
Deprecated: Return type of Template::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/eQN3R on line 77 Deprecated: Return type of Template::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/eQN3R on line 76 Deprecated: Return type of Template::offsetSet($offset, $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/eQN3R on line 66 Deprecated: Return type of Template::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/eQN3R on line 78 <pre><code>Template Object ( [implied_key_list] => Array ( ) [noises] => Array ( [dog] => woof ) ) </code></pre>
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.36, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.29 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
<pre><code>Template Object ( [implied_key_list] => Array ( ) [noises] => Array ( [dog] => woof ) ) </code></pre>
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/eQN3R on line 62
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/eQN3R on line 50
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
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/eQN3R on line 50
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/eQN3R on line 50
Process exited with code 255.

preferences:
215.07 ms | 401 KiB | 346 Q