#46461 [Com]: After a array key is referenced in a class variable, the array can't be copied.

From: Date: Sat, 01 Nov 2008 23:06:52 +0000
Subject: #46461 [Com]: After a array key is referenced in a class variable, the array can't be copied.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-130444@lists.php.net to get a copy of this message
ID: 46461 Comment by: please at dontemailme dot com Reported By: twonests at yahoo dot com Status: Open Bug Type: Arrays related Operating System: linux, centos 5.0 PHP Version: 5.2.6 New Comment: By the way your "user at site dot com" isn't fooling ANY spam spiders at this point. The know that's an email address. If I'd known that I wouldn't have posted my email address. And the edit form wont let me change it. Someone remove my email please. Previous Comments: ------------------------------------------------------------------------ [2008-11-01 22:38:43] twonests at yahoo dot com Description: ------------ If I make an array, (NOT a class, an array with strings in it), and then one of the keys of that array is referenced by a class instance's member variable, then that array key/value can no longer be copied. See the code, it's small. We have php 5.2.3. No, sorry, we can't upgrade to check if it's been fixed. Try running the code, it's small. Your form did not provide me the option of saying so. (Which I think is unreasonable.) Reproduce code: --------------- class c { private $ref; public function setref(&$r) { $this->ref = &$r; } } $ar = array('a'=>'b'); $c = new c(); $c->setref( $ar['a']); $copy = $ar; $copy['a'] = 'x'; print_r($ar); /* This will print: Array ( [a] => x ) (Which it shouldn't.) As a demonstration, try commenting out the line $c->setref( $ar['a']); You'll get a different result. (Which you shouldn't.) */ Expected result: ---------------- Array ( [a] => b ) Notice that the line $copy = $ar; is making a copy, not a reference. Actual result: -------------- This will print: Array ( [a] => x ) (Which it shouldn't.) As a demonstration, try commenting out the line $c->setref( $ar['a']); You'll get a different result. (Which you shouldn't.) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=46461&edit=1

« previous php.bugs (#130444) next »