xref: /PHP-8.3/ext/spl/tests/array_019.phpt (revision c6357b80)
1--TEST--
2SPL: ArrayIterator and foreach by reference
3--FILE--
4<?php
5
6$ar = new ArrayObject(array(1));            foreach($ar as &$v) var_dump($v);
7$ar = new ArrayIterator(array(2));          foreach($ar as &$v) var_dump($v);
8$ar = new RecursiveArrayIterator(array(3)); foreach($ar as &$v) var_dump($v);
9
10class ArrayIteratorEx extends ArrayIterator
11{
12    function current(): mixed
13    {
14        return ArrayIterator::current();
15    }
16}
17
18$ar = new ArrayIteratorEx(array(4)); foreach($ar as $v) var_dump($v);
19$ar = new ArrayIteratorEx(array(5)); foreach($ar as &$v) var_dump($v);
20
21?>
22===DONE===
23--EXPECTF--
24int(1)
25int(2)
26int(3)
27int(4)
28
29Fatal error: Uncaught Error: An iterator cannot be used with foreach by reference in %s:%d
30Stack trace:
31#0 {main}
32  thrown in %s on line %d
33