php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37744 unseting an index on ArrayObject may modify the original array
Submitted: 2006-06-08 13:12 UTC Modified: 2006-06-19 17:06 UTC
From: eric dot daspet at survol dot net Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.1.4 OS: MS Windows XP
Private report: No CVE-ID: None
 [2006-06-08 13:12 UTC] eric dot daspet at survol dot net
Description:
------------
Deleting an index in ArrayObjet also delete it from the original array *if and only if* ArrayObject was not modified.

This may introduce bugs (unexpected deletion in the original array).



Note: if modification of the original array is wanted behaviour, then we have the opposite bugs :
- documentation does not says that the array in ArrayObject constructor is passed as reference
- I we modify the value of some entry in ArrayObject, the the relation between ArrayObject and the original array is lost 

Reproduce code:
---------------
<?php

$array = array('foo' => 'bar') ;
$object = new ArrayObject( $array ) ;


// uncomment this for opposite behaviour 
// and loose the reference between $object and $array
// $objet['foo'] = 'hello' ;


unset($objet['foo']);
var_dump( $array ) ;

Expected result:
----------------
array(1) { ["foo"]=>  string(3) "bar" }

Actual result:
--------------
array(0) {  }

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-06-19 17:06 UTC] tony2001@php.net
Code:
<?php
$array = array('foo' => 'bar') ;
$object = new ArrayObject( $array ) ;
unset($object['foo']);
var_dump( $array ) ;
?>

Output:
array(0) {
}
Code:
<?php
$array = array('foo' => 'bar') ;
$object = new ArrayObject( $array ) ;
$object['foo'] = 1;;
unset($object['foo']);
var_dump( $array ) ;
?>

Output:
array(0) {
}

Note the "objeCt" and "objet" in your code.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC