#44559 [Bgs]: Array keys being added when returning by reference
| From: | kurt at surfmerchants dot com | Date: | Tue, 01 Apr 2008 16:45:46 +0000 |
| Subject: | #44559 [Bgs]: Array keys being added when returning by reference | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-123828@lists.php.net to get a copy of this message | ||
ID: 44559
User updated by: kurt at surfmerchants dot com
Reported By: kurt at surfmerchants dot com
Status: Bogus
Bug Type: Arrays related
Operating System: Linux
PHP Version: 5.2.5
New Comment:
Yeah, I see that now in the docs, thanks.
However, just because it's in the docs doesn't make it "right" -- this
behavior is *very* confusing when it happens. I wonder if any other
language does this; it basically means that you can't count on the
contents of any array being correct when you iterate over it, unless you
created that array yourself are the only one that has accessed it.
Previous Comments:
------------------------------------------------------------------------
[2008-03-29 19:48:13] felipe@php.net
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
Says the documentation:
"Note: If you assign, pass or return an undefined variable by
reference, it will get created."
http://docs.php.net/manual/en/language.references.whatdo.php
------------------------------------------------------------------------
[2008-03-28 14:05:42] kurt at surfmerchants dot com
Description:
------------
When returning an array element from a function by reference, if the
element key does not exist, the element key is added.
Reproduce code:
---------------
class simple
{
var $items = array();
function &getItem($name)
{
return $this->items[$name];
}
}
$obj = new simple();
print "Keys before getting item:\n".print_r(array_keys($obj->items),
TRUE)."\n";
$retrieved =& $obj->getItem('SHOULD_NOT_EXIST');
print "Retrieved: \n";
var_dump($retrieved);
print "Keys after getting item:\n".print_r(array_keys($obj->items),
TRUE)."\n";
Expected result:
----------------
Keys before getting item:
Array
(
)
Retrieved:
NULL
Keys after getting item:
Array
(
)
Actual result:
--------------
Keys before getting item:
Array
(
)
Retrieved:
NULL
Keys after getting item:
Array
(
[0] => SHOULD_NOT_EXIST
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=44559&edit=1