Req #51992 [Bgs]: ArrayAccess and references
| From: | andrewm dot finewolf at gmail dot com | Date: | Thu, 03 Jun 2010 21:29:30 +0000 |
| Subject: | Req #51992 [Bgs]: ArrayAccess and references | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-151069@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=51992&edit=1
ID: 51992
User updated by: andrewm dot finewolf at gmail dot com
Reported by: andrewm dot finewolf at gmail dot com
Summary: ArrayAccess and references
Status: Bogus
Type: Feature/Change Request
Package: SPL related
Operating System: Irrelevant
PHP Version: Irrelevant
New Comment:
And none of those where filled in "Feature/Change Request"...
Previous Comments:
------------------------------------------------------------------------
[2010-06-03 23:28:19] johannes@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same.
Thank you for your interest in PHP.
This limitation is subject of a few other bugs and discussions.
------------------------------------------------------------------------
[2010-06-03 23:19:13] andrewm dot finewolf at gmail dot com
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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=51992&edit=1