3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Collection extends ArrayObject { private $data; function __construct() { $this->data = array(); parent::__construct($this->data); } function offsetGet($index) { echo __METHOD__ . "($index)\n"; return parent::offsetGet($index); } function offsetSet($index, $value) { echo __METHOD__ . "(" . (is_null($index) ? "NULL" : $index) . ",$value)\n"; parent::offsetSet($index, $value); } } echo "\n\nInitiate Obj\n"; $arrayObj = new Collection(); echo "Assign values\n"; $arrayObj[] = "foo"; var_dump($arrayObj[0]); $arrayObj[] = "bar"; var_dump($arrayObj[0]); var_dump($arrayObj[1]); $arrayObj["foo"] = "baz"; var_dump($arrayObj["foo"]); print_r($arrayObj); var_dump(count($arrayObj));
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Return type of Collection::offsetGet($index) should either be compatible with ArrayObject::offsetGet(mixed $key): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Gi6SO on line 13 Deprecated: Return type of Collection::offsetSet($index, $value) should either be compatible with ArrayObject::offsetSet(mixed $key, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Gi6SO on line 19 Initiate Obj Assign values Collection::offsetSet(NULL,foo) Collection::offsetGet(0) string(3) "foo" Collection::offsetSet(NULL,bar) Collection::offsetGet(0) string(3) "foo" Collection::offsetGet(1) string(3) "bar" Collection::offsetSet(foo,baz) Collection::offsetGet(foo) string(3) "baz" Collection Object ( [data:Collection:private] => Array ( ) [storage:ArrayObject:private] => Array ( [0] => foo [1] => bar [foo] => baz ) ) int(3)
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.7, 7.2.29 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
Initiate Obj Assign values Collection::offsetSet(NULL,foo) Collection::offsetGet(0) string(3) "foo" Collection::offsetSet(NULL,bar) Collection::offsetGet(0) string(3) "foo" Collection::offsetGet(1) string(3) "bar" Collection::offsetSet(foo,baz) Collection::offsetGet(foo) string(3) "baz" Collection Object ( [data:Collection:private] => Array ( ) [storage:ArrayObject:private] => Array ( [0] => foo [1] => bar [foo] => baz ) ) int(3)
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Initiate Obj Assign values Collection::offsetSet(NULL,foo) Collection::offsetGet(0) string(3) "foo" Collection::offsetSet(NULL,bar) Collection::offsetGet(0) string(3) "foo" Collection::offsetGet(1) string(3) "bar" Collection::offsetSet(foo,baz) Collection::offsetGet(foo) string(3) "baz" Collection Object ( [0] => foo [1] => bar [foo] => baz ) int(3)
Output for 5.0.4 - 5.0.5
Initiate Obj Assign values
Process exited with code 139.
Output for 5.0.0 - 5.0.3
Initiate Obj Assign values string(3) "foo" string(3) "foo" string(3) "bar" string(3) "baz" Collection Object ( [0] => foo [1] => bar [foo] => baz ) int(3)
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Gi6SO on line 5
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 T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Gi6SO on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/Gi6SO on line 5
Process exited with code 255.

preferences:
247.09 ms | 401 KiB | 308 Q