php-src/tests/lang/foreachLoop.010.phpt

41 lines
1.2 KiB
Plaintext
Raw Normal View History

--TEST--
This test illustrates the impact of invoking destructors when refcount is decremented to 0 on foreach.
It will pass only if the 'contentious code' in PHPValue.decReferences() is enabled.
--FILE--
<?php
$a = array(1,2,3);
$container = array(&$a);
2018-10-14 16:03:31 +00:00
// From php.net:
// "Unless the array is referenced, foreach operates on a copy of
// the specified array and not the array itself."
// At this point, the array $a is referenced.
// The following line ensures $a is no longer references as a consequence
2018-10-14 16:03:31 +00:00
// of running the 'destructor' on $container.
$container = null;
// At this point the array $a is no longer referenced, so foreach should operate on a copy of the array.
// However, P8 does not invoke 'destructors' when refcount is decremented to 0.
// Consequently, $a thinks it is still referenced, and foreach will operate on the array itself.
// This provokes a difference in behaviour when changing the number of elements in the array while
2018-10-14 16:03:31 +00:00
// iterating over it.
$i=0;
foreach ($a as $v) {
2020-02-03 21:52:20 +00:00
array_push($a, 'new');
var_dump($v);
2018-09-16 17:16:42 +00:00
2020-02-03 21:52:20 +00:00
if (++$i>10) {
echo "Infinite loop detected\n";
break;
}
}
?>
--EXPECT--
int(1)
int(2)
int(3)