File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / tests / lang / foreachLoop.010.phpt
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 23:48:06 2012 UTC (12 years, 4 months ago) by misho
Branches: php, MAIN
CVS tags: v5_4_3elwix, v5_4_29p0, v5_4_29, v5_4_20p0, v5_4_20, v5_4_17p0, v5_4_17, v5_3_10, HEAD
php

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>