Req #51622 [Com]: ArrayObject::offsetGet should return a reference

From: Date: Thu, 18 Nov 2021 06:10:31 +0000
Subject: Req #51622 [Com]: ArrayObject::offsetGet should return a reference
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237834@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=51622&edit=1 ID: 51622 Comment by: kauramanat238 at gmail dot com Reported by: oliver dot graetz at gmx dot de Summary: ArrayObject::offsetGet should return a reference Status: Open Type: Feature/Change Request Package: SPL related PHP Version: 5.2.13 Block user comment: N Private report: N New Comment: We All4pets came with a vision to make pet parenting hassle free. Getting a pet is like getting a child they need our care concern and also all the basic needs <a href="https://www.all4pets.in/products/pet-accessories/dog-cages/">dog cage</a> Previous Comments: ------------------------------------------------------------------------ [2013-04-16 04:54:33] me at achronos dot ca Although no php dev has ever mentioned it in a bug report (that I can find), this issue has in fact been fixed. At least, it has been fixed for ArrayAccess. For ArrayObject, I haven't tested. According to the PHP docs (http://php.net/manual/en/arrayaccess.offsetget.php notes section) this was fixed in 5.3.4 by relaxing the requirements for implementing AccessAccess, so that public function &offsetGet($offset) {} works, and thus indirect access works. I've confirmed this does in fact work (on 5.3.8). There are multiple bug reports all related to this topic: https://bugs.php.net/bug.php?id=51622 https://bugs.php.net/bug.php?id=34783 https://bugs.php.net/bug.php?id=32983 ------------------------------------------------------------------------ [2011-02-18 23:35:18] urkle at outoforder dot cc Is this issue fixed in PHP 5.3.4 which supports defining &offsetGet ? (As documented in http://php.net/manual/en/arrayaccess.offsetget.php ) Actually Just tested and it's not.. Though ArrayAccess by itself works wonderfully. --- Test script (Tested on PHP 5.3.5 x86_64 on Fedora 14) <?php class Test implements ArrayAccess { private $data = array(); /** Array Access */ public function offsetExists($offset) { return isset($this->data[$offset]); } public function &offsetGet($offset) { return $this->data[$offset]; } public function offsetSet($offset, $value) { $this->data[$offset] = $value; } public function offsetUnset($offset) { unset($this->data[$offset]); } } class Test2 extends ArrayObject { public function &offsetGet($offset) { $t =& parent::offsetGet($offset); return $t; } } $t = new Test(); $t['test'] = array(1,2,3,4); echo serialize($t)."\n"; $t['test'][] = 5; echo serialize($t)."\n"; $t = new Test2(); $t['test'] = array(1,2,3,4); echo serialize($t)."\n"; $t['test'][] = 5; echo serialize($t)."\n"; ------------------------------------------------------------------------ [2010-08-18 10:21:25] golgote at mamasam dot com 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. ------------------------------------------------------------------------ [2010-06-03 23:32:03] andrewm dot finewolf at gmail dot com Arrays in PHP contains references to primitive types and reference types. Which basically means that if you are post-incrementing an element, well, it actually works. Why is ArrayAccess::offsetGet() returns by value instead of by reference? Wasn't ArrayAccess created to emulate an array? This is a major inconsistency in the platform and makes this whole interface pretty useless. This isn't an engine limitation. __get(), __set(), __isset(), __unset() is returning values by reference without any problems. Why can't ArrayAccess (when it does pretty much the same thing?) Can ArrayAccess::offsetGet() return by reference (or at the very least create a second interface, "ArrayAccessRef", for this)? ------------------------------------------------------------------------ [2010-04-27 10:02:34] colder@php.net 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. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=51622 -- Edit this bug report at https://bugs.php.net/bug.php?id=51622&edit=1

« previous php.bugs (#237834) next »