Bug->Req #51622 [Opn]: ArrayObject::offsetGet should return a reference
| From: | colder@php.net | Date: | Tue, 27 Apr 2010 08:02:34 +0000 |
| Subject: | Bug->Req #51622 [Opn]: ArrayObject::offsetGet should return a reference | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-149944@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=51622&edit=1
ID: 51622
Updated by: colder@php.net
Reported by: oliver dot graetz at gmx dot de
-Summary: ArrayObject shows inconsistent behaviour
+Summary: ArrayObject::offsetGet should return a reference
Status: Open
-Type: Bug
+Type: Feature/Change Request
Package: SPL related
PHP Version: 5.2.13
New Comment:
This is actually a feature request. To be backward compatible, we would
need to
have a new interface, and obviously a new interface only to solve that
reference
thing is painful.
The immediate solution is to return an ArrayObject itself, which means
that
indirect modifications will work.
Previous Comments:
------------------------------------------------------------------------
[2010-04-21 13:35:50] oliver dot graetz at gmx dot de
Description:
------------
This bug refers to my report filed under
http://bugs.php.net/bug.php?id=34783 which is now
more than four years
old. In the meantime I found out that using ArrayObject instead of the
test class the
$t['huba'][]='three';
actually works, thanks to the SPL using its "implemented in C advantage"
to circumvent the problem. Actually, it works until the programmer
decides to inherit from ArrayObject and overwrite offsetGet(). Then the
problem of the offsetGet() method not returning by reference is back.
Back in 2005 you were very quick to flag the report as BOGUS, but a look
at the source code of "zend_interfaces.c" proves that there is in fact a
problem:
ZEND_BEGIN_ARG_INFO_EX(arginfo_arrayaccess_offset_get, 0, 0, 1) /*
actually this should be return by ref but atm cannot be */
The best way of dealing with this is not to mark it as BOGUS and deny
that there is a problem. It would be admitting the fault and perhaps
introducing an alternative NewArrayAccess interface that defines
&offsetGet(). So future code can use it without breaking old
implementations.
Test script:
---------------
<?php
class Test1 extends ArrayObject
{
}
class Test2 extends ArrayObject
{
function offsetGet($key) { return parent::offsetGet($key); }
}
$t1 = new Test1();
$t1['huba'] = array('one','two');
$t1['huba'][] = 'three';
print_r($t1);
$t2 = new Test2();
$t2['huba'] = array('one','two');
$t2['huba'][] = 'three';
print_r($t2);
Expected result:
----------------
Test1 Object
(
[huba] => Array
(
[0] => one
[1] => two
[2] => three
)
)
Test2 Object
(
[huba] => Array
(
[0] => one
[1] => two
[2] => three
)
)
Actual result:
--------------
Test1 Object
(
[huba] => Array
(
[0] => one
[1] => two
[2] => three
)
)
Notice: Indirect modification of overloaded element of Test2 has no
effect in F:\huba.php on line 17
Test2 Object
(
[huba] => Array
(
[0] => one
[1] => two
)
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=51622&edit=1