|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2010-06-03 23:28 UTC] johannes@php.net
-Status: Open
+Status: Bogus
[2010-06-03 23:28 UTC] johannes@php.net
[2010-06-03 23:29 UTC] andrewm dot finewolf at gmail dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 04:00:02 2025 UTC |
Description: ------------ 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)? Test script: --------------- <?php class ArrayTest implements ArrayAccess { private $container = array(); public function offsetSet($name, $value) { $this->container[$name] = $value; } public function offsetExists($name) { return isset($this->container[$name]); } public function offsetUnset($name) { unset($this->container[$name]); } public function offsetGet($name) { return isset($this->container[$name]) ? $this->container[$name] : null; } } class ObjectTest { private $container = array(); public function __set($name, $value) { $this->container[$name] = $value; } public function __isset($name) { return isset($this->container[$name]); } public function __unset($name) { unset($this->container[$name]); } public function __get($name) { return isset($this->container[$name]) ? $this->container[$name] : null; } } $array = array(); $array['counter'] = 0; $array['counter']++; echo $array['counter'] . "\n"; $objArray = new ArrayTest(); $objArray['counter'] = 0; $objArray['counter']++; echo $objArray['counter'] . "\n"; $objAccess = new ObjectTest(); $objAccess->counter = 0; $objAccess->counter++; echo $objAccess->counter . "\n"; Expected result: ---------------- 1 1 1 Actual result: -------------- 1 0 1