php.net |  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
 [2010-06-03 23:19 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-03 23:28 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-06-03 23:28 UTC] 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: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: Tue Jan 22 16:01:28 2019 UTC