From:
Operating system: Irrelevant
PHP version: Irrelevant
Package: SPL related
Bug Type: Feature/Change Request
Bug description:ArrayAccess and references
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 bug report at http://bugs.php.net/bug.php?id=51992&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=51992&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=51992&r=trysnapshot53
Try a snapshot (trunk):
http://bugs.php.net/fix.php?id=51992&r=trysnapshottrunk
Fixed in SVN:
http://bugs.php.net/fix.php?id=51992&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=51992&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=51992&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=51992&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=51992&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=51992&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=51992&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=51992&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=51992&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=51992&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=51992&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=51992&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=51992&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=51992&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=51992&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=51992&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=51992&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=51992&r=mysqlcfg