3v4l.org

run code in 300+ PHP versions simultaneously
<?php class DataContainer implements IteratorAggregate { protected $data; public function __construct(array $data) { $this->data = $data; } public function &getIterator() { foreach ($this->data as $key => &$value) { yield $key => $value; } } } The class can then be iterated using by-ref foreach: $dataContainer = new DataContainer([1, 2, 3]); foreach ($dataContainer as &$value) { $value *= -1; } var_dump($dataContainer); // $this->data is now [-1, -2, -3]
Output for 5.4.0 - 5.4.22
Parse error: syntax error, unexpected '$key' (T_VARIABLE) in /in/47DG7 on line 11
Process exited with code 255.
Output for 5.3.0 - 5.3.27
Parse error: syntax error, unexpected T_VARIABLE in /in/47DG7 on line 11
Process exited with code 255.

preferences:
174.43 ms | 1395 KiB | 58 Q