php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42065 SPL ArrayObject does not use "copy-on-write"
Submitted: 2007-07-22 11:05 UTC Modified: 2007-07-23 07:45 UTC
From: a dot yamanoi at gmail dot com Assigned:
Status: Closed Package: SPL related
PHP Version: 5.2.3 OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: a dot yamanoi at gmail dot com
New email:
PHP Version: OS:

 

 [2007-07-22 11:05 UTC] a dot yamanoi at gmail dot com
Description:
------------
When ArrayObject::offsetSet() method stores a new value into an array,
I expect ArrayObject to change only the array which is given as a constructor parameter.

However, if the array is copied from another array and unchanged,
ArrayObject will also change the another array.

I thought it was a "copy-on-write" problem.

thank you for any help
Please excuse my English, it isn't very good.

Reproduce code:
---------------
$base = array('key' => 'old value');

$copy = $base;
$ao = new ArrayObject($copy);
$ao['key'] = 'new value';

echo '$base["key"] = ', $base['key'], "\n";
echo '$copy["key"] = ', $copy['key'], "\n";
echo '$ao["key"] = ', $ao['key'], "\n";

Expected result:
----------------
$base["key"] = old value
$copy["key"] = new value
$ao["key"] = new value

Actual result:
--------------
$base["key"] = new value
$copy["key"] = new value
$ao["key"] = new value

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-22 15:34 UTC] jani@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows (zip):
 
  http://snaps.php.net/win32/php5.2-win32-latest.zip

For Windows (installer):

  http://snaps.php.net/win32/php5.2-win32-installer-latest.msi

This might have already been fixed..
 [2007-07-23 07:45 UTC] a dot yamanoi at gmail dot com
Thank you very much for your prompt reply.

It is fixed in the latest snapshot.

> php -v
PHP 5.2.4-dev (cli) (built: Jul 23 2007 00:04:07)

Expected result:
----------------
$base["key"] = old value
$copy["key"] = old value   <--- (In my first entry, this line was wrong.)
$ao["key"] = new value

I can get the expected result!
Thank you very much for your time.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jun 29 17:01:32 2025 UTC