#44559 [Opn->Bgs]: Array keys being added when returning by reference

From: Date: Sat, 29 Mar 2008 19:48:13 +0000
Subject: #44559 [Opn->Bgs]: Array keys being added when returning by reference
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-123769@lists.php.net to get a copy of this message
ID: 44559 Updated by: felipe@php.net Reported By: kurt at surfmerchants dot com -Status: Open +Status: Bogus Bug Type: Arrays related Operating System: Linux PHP Version: 5.2.5 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 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 Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#123769) next »