|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2004-06-18 16:19 UTC] nospam0 at malkusch dot de
 Description:
------------
If I save an ArrayObject in an ArrayObject, I can't 
manipulate the stored ArrayObject, by getting it with 
offsetGet() 
Reproduce code:
---------------
$array     = new ArrayObject();
$subArray  = new ArrayObject();
$array->append($subArray);
$subArray->append('item');
var_dump($array);
$array->offsetGet(0)->append('item2');
var_dump($array);
Expected result:
----------------
object(ArrayObject)#23 (1) { 
  [0]=> object(ArrayObject)#24 (1) { 
    [0]=> string(4) "item" } } 
 
oobject(ArrayObject)#23 (1) { 
  [0]=> object(ArrayObject)#24 (1) { 
    [0]=> string(4) "item", 
    [1]=> string(5) "item2" } } 
Actual result:
--------------
object(ArrayObject)#23 (1) { 
  [0]=> object(ArrayObject)#24 (1) { 
    [0]=> string(4) "item" } } 
 
object(ArrayObject)#23 (1) { [0]=> NULL } 
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Tue Oct 28 13:00:01 2025 UTC | 
> Try this code: > <?php > > $array = new ArrayObject(); > $subArray = new ArrayObject(); > $array->append($subArray); > > $subArray->append(\'item\'); > var_dump($array); > > $x=$array->offsetGet(0); > $x->append(\'item2\'); > var_dump($array); > var_dump($x); > > ?> I don't think, that the output is expected: object(ArrayObject)#1 (1) { [0]=> object(ArrayObject)#2 (1) { [0]=> string(4) "item" } } object(ArrayObject)#1 (1) { [0]=> NULL } object(ArrayObject)#2 (2) { [0]=> string(4) "item" [1]=> string(5) "item2" } Why is there a NULL in the second var_dump? If offsetGet() would not return a reference I would at least expect, that my ArrayObject won't loose its entry. It seems that offsetGet() does the work of offsetUnset().> Returning "not a reference" = "creating a copy". That's > why. That's why offsetGet deletes the entry? Perhaps you didn't get what I mean and actually the subject for this bug is wrong, so I changed it (Sorry I was fixed on the 2D ArrayObject). Try: <?php $array = new ArrayObject(); $array->append('test'); var_dump($array); $array->offsetGet(0); var_dump($array); ?> It will return object(ArrayObject)#1 (1) { [0]=> string(4) "test" } object(ArrayObject)#1 (1) { [0]=> NULL } So, In my eyes it is a bug, that offsetGet deletes the entry from the ArrayObject. It should return a copy (or better a reference) and not touch the ArrayObject.