|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51992 ArrayAccess and references
Submitted: 2010-06-03 23:19 UTC Modified: 2010-06-03 23:29 UTC
From: andrewm dot finewolf at gmail dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: andrewm dot finewolf at gmail dot com
New email:
PHP Version: OS:


 [2010-06-03 23:19 UTC] 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)?

Test script:
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) {
	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) {
	public function __get($name) {
		return isset($this->container[$name]) ? $this->container[$name] : null;

$array = array();
$array['counter'] = 0;
echo $array['counter'] . "\n";

$objArray = new ArrayTest();
$objArray['counter'] = 0;
echo $objArray['counter'] . "\n";

$objAccess = new ObjectTest();
$objAccess->counter = 0;
echo $objAccess->counter . "\n";

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-03 23:28 UTC]
-Status: Open +Status: Bogus
 [2010-06-03 23:28 UTC]
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:29 UTC] andrewm dot finewolf at gmail dot com
And none of those where filled in "Feature/Change Request"...
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Jun 24 09:01:26 2019 UTC