php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46461 After a array key is referenced in a class variable, the array can't be copied.
Submitted: 2008-11-01 22:38 UTC Modified: 2008-11-02 01:11 UTC
From: user at wanted dot email dot removed Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.3 OS: linux, centos 5.0
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: user at wanted dot email dot removed
New email:
PHP Version: OS:

 

 [2008-11-01 22:38 UTC] user at wanted dot email dot removed
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.)
 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-02 01:09 UTC] jani@php.net
This is in so many ways bogus..
 [2008-11-02 01:11 UTC] user at wanted dot email dot removed
Um how is this "bogus"? Elaborate?
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 19:01:25 2019 UTC