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 git.master, git.master_jit, rfc.property-hooks
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>

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:
60.13 ms | 404 KiB | 8 Q