<?php
class DataContainer implements IteratorAggregate { // create an external Iterator:
protected $data;
public function __construct(array $data) {
$this->data = $data;
}
public function &getIterator() {
foreach ($this->data as &$value) {
yield $value;
}
}
}
$dataContainer = new DataContainer([77, 78, 79]);
foreach ($dataContainer as &$value) {
echo $value *= -1,"\n";
}
// Weird result from neglecting to unset $value ...
foreach($dataContainer as $value);
var_dump($dataContainer);
Deprecated: Return type of & DataContainer::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/rK9Ff on line 9
-77
-78
-79
object(DataContainer)#1 (1) {
["data":protected]=>
array(3) {
[0]=>
int(-77)
[1]=>
int(-78)
[2]=>
&int(-78)
}
}