ID: 45170 Comment by: crrodriguez at suse dot de Reported By: johnny at eiole dot com Status: Open Bug Type: Class/Object related Operating System: Windows PHP Version: 5.2.6 New Comment:
key(testbug::getTab()) inside foreach produces undefined behaviuor, do not mix foreach with next(), key(), current() etc.. it is known to produce "strange but correct" results. Previous Comments: ------------------------------------------------------------------------ [2008-06-04 11:54:39] johnny at eiole dot com Description: ------------ When an static array if retourned from a method, for iterating, and acceded with the key() function, the reference pointer is moved to the end of the array. After the loop, the array is unaccessible. Important : if you add the following line : $tab = testbug::getTab(); before the loop, the error does not occur. you can use key(), current(), prev(), there is still a bug. First tested on windows 5.2.5 version, and still present on 5.2.6 version. There is no bug on 5.2.3 version. Thanks for answer. Reproduce code: --------------- <?php class testbug { static $tab = array( 'k1' => "elm1", 'k2' => "elm2", 'k3' => "elm3", 'k4' => "elm4", ); static function getTab() { return self::$tab; } } var_dump(testbug::getTab()); foreach (testbug::getTab() as $k => $v) { key(testbug::getTab()); } var_dump(testbug::getTab()); Expected result: ---------------- array 'k1' => string 'elm1' (length=4) 'k2' => string 'elm2' (length=4) 'k3' => string 'elm3' (length=4) 'k4' => string 'elm4' (length=4) array 'k1' => string 'elm1' (length=4) 'k2' => string 'elm2' (length=4) 'k3' => string 'elm3' (length=4) 'k4' => string 'elm4' (length=4) Actual result: -------------- array 'k1' => string 'elm1' (length=4) 'k2' => string 'elm2' (length=4) 'k3' => string 'elm3' (length=4) 'k4' => string 'elm4' (length=4) null ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=45170&edit=1