1--TEST-- 2Generator methods can yield by reference 3--FILE-- 4<?php 5 6class Test implements IteratorAggregate { 7 protected $data; 8 9 public function __construct(array $data) { 10 $this->data = $data; 11 } 12 13 public function getData() { 14 return $this->data; 15 } 16 17 public function &getIterator(): Traversable { 18 foreach ($this->data as $key => &$value) { 19 yield $key => $value; 20 } 21 } 22} 23 24$test = new Test([1, 2, 3, 4, 5]); 25foreach ($test as &$value) { 26 $value *= -1; 27} 28 29var_dump($test->getData()); 30 31?> 32--EXPECT-- 33array(5) { 34 [0]=> 35 int(-1) 36 [1]=> 37 int(-2) 38 [2]=> 39 int(-3) 40 [3]=> 41 int(-4) 42 [4]=> 43 &int(-5) 44} 45