php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53723 ArrayAccess not works properly on multi-level arrays
Submitted: 2011-01-12 14:04 UTC Modified: 2011-01-12 20:55 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: zsolt94 at gmail dot com Assigned:
Status: Duplicate Package: Class/Object related
PHP Version: 5.2.17 OS: Ubuntu Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: zsolt94 at gmail dot com
New email:
PHP Version: OS:

 

 [2011-01-12 14:04 UTC] zsolt94 at gmail dot com
Description:
------------
If I use an Object, with an ArrayAccess interface, I can't modify element arrays the same way like 2-dimensional arrays

Test script:
---------------
<?php
// Just a simple class, for testing.
class ArrayTest implements ArrayAccess
{
	private $array;
	
	public function offsetGet($offset) {
		return $this->array[$offset];
	}
	
	public function offsetSet($offset,$value) {
		$this->array[$offset] = $value;
	}
	
	public function offsetUnset($offset) {
		unset($this->array[$offset]);
	}
	
	public function offsetExists($offset) {
		return isset($this->array[$offset]);
	}
}

$a = new ArrayTest();
$b = new ArrayTest();

$a['numbers'] = array('one'=>1);
var_dump($a['numbers']);

$b['numbers']['one'] = 1;
var_dump($b['numbers']);
?>



Expected result:
----------------
I want to get:

array ( 'one' => 1, )
array ( 'one' => 1, )

But I got instead:

array ( 'one' => 1, )
Notice: Indirect modification of overloaded element of ArrayTest has no effect in /home/zsolt94/www/oopy/arraytest.php on line 30
null

I think that:
$a['foo']['bar'] = $anything;

Should do the same effect as that:

$foo = $a['foo'];
$foo['bar'] = $anything;
$a['foo'] = $foo;
unset($foo);


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-12 20:55 UTC] cataphract@php.net
-Status: Open +Status: Duplicate
 [2011-01-12 20:55 UTC] cataphract@php.net
Duplicate/fixed in PHP 5.3.4, where you can make offsetGet return by ref.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 17:01:34 2025 UTC