xref: /PHP-7.4/ext/spl/tests/array_019.phpt (revision ede663f3)
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()
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<?php exit(0); ?>
24--EXPECTF--
25int(1)
26int(2)
27int(3)
28int(4)
29
30Fatal error: Uncaught RuntimeException: An iterator cannot be used with foreach by reference in %s:%d
31Stack trace:
32#0 {main}
33  thrown in %s on line %d
34