#45170 [Opn->Bgs]: Reference and key() problem
| From: | jani@php.net | Date: | Sun, 06 Jul 2008 11:56:37 +0000 |
| Subject: | #45170 [Opn->Bgs]: Reference and key() problem | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-126315@lists.php.net to get a copy of this message | ||
ID: 45170
Updated by: jani@php.net
Reported By: johnny at eiole dot com
-Status: Open
+Status: Bogus
Bug Type: Class/Object related
Operating System: Windows
PHP Version: 5.2.6
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Previous Comments:
------------------------------------------------------------------------
[2008-06-06 18:52:16] crrodriguez at suse dot de
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.
------------------------------------------------------------------------
[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