php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28831 ArrayObject::offsetGet() does the work of offsetUnset()
Submitted: 2004-06-18 16:19 UTC Modified: 2004-06-20 18:47 UTC
From: nospam0 at malkusch dot de Assigned: helly (profile)
Status: Closed Package: Arrays related
PHP Version: 5.0.0RC3 OS: *
Private report: No CVE-ID: None
 [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 } 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-18 20:55 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Unfortunatley internal functions cannot return a reference.......

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);

?>
 [2004-06-20 05:19 UTC] nospam0 at malkusch dot de
> 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().
 [2004-06-20 11:44 UTC] helly@php.net
Returning "not a reference" = "creating a copy". That's why. Hopefully we are able to return references in 5.1 so we can solve this then
 [2004-06-20 16:48 UTC] nospam0 at malkusch dot de
> 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.
 [2004-06-20 18:47 UTC] helly@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 11:01:30 2024 UTC