foreachLoop.010.phpt 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. --TEST--
  2. This test illustrates the impact of invoking destructors when refcount is decremented to 0 on foreach.
  3. It will pass only if the 'contentious code' in PHPValue.decReferences() is enabled.
  4. --FILE--
  5. <?php
  6. $a = array(1,2,3);
  7. $container = array(&$a);
  8. // From php.net:
  9. // "Unless the array is referenced, foreach operates on a copy of
  10. // the specified array and not the array itself."
  11. // At this point, the array $a is referenced.
  12. // The following line ensures $a is no longer references as a consequence
  13. // of running the 'destructor' on $container.
  14. $container = null;
  15. // At this point the array $a is no longer referenced, so foreach should operate on a copy of the array.
  16. // However, P8 does not invoke 'destructors' when refcount is decremented to 0.
  17. // Consequently, $a thinks it is still referenced, and foreach will operate on the array itself.
  18. // This provokes a difference in behaviour when changing the number of elements in the array while
  19. // iterating over it.
  20. $i=0;
  21. foreach ($a as $v) {
  22. array_push($a, 'new');
  23. var_dump($v);
  24. if (++$i>10) {
  25. echo "Infinite loop detected\n";
  26. break;
  27. }
  28. }
  29. ?>
  30. --EXPECT--
  31. int(1)
  32. int(2)
  33. int(3)