|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-04-21 13:35 UTC] 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 ) ) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 23:00:01 2025 UTC |
I'd like to see a solution to this problem as well. I have spent countless hours trying to use ArrayObject and ArrayAccess and they both have problems either with get and unset. An unset() on an ArrayObject issues the notice : $object["list"][0]["prods"] = "1,3"; $object["list"][0]["cache"][1] = array( 'name' => 'p3', 'categories' => array( array('category' => 'c3'), ), 'price' => 3 ); unset($object["list"][0]["cache"][2]); will issue Notice: Indirect modification of overloaded element... while this works with ArrayAccess! But something like a straight : $object['arr'][0]['foo'] = 'bar'; will issue the notice with ArrayAccess but not with an ArrayObject! It's really silly. I suggest that since this bug hasn't been fixed since 2005, it should be documented clearly that neither ArrayAccess and ArrayObject work correctly with multidimensional arrays and are just quick, funny but useless hacks so that people stop wasting their time with them.