php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52540 Error on update array's field through __set magic method
Submitted: 2010-08-05 09:43 UTC Modified: 2010-08-05 11:02 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: trong at ngs dot ru Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.3.3 OS: Windows XP
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: trong at ngs dot ru
New email:
PHP Version: OS:

 

 [2010-08-05 09:43 UTC] trong at ngs dot ru
Description:
------------
If I use __set magic method for arrays and try to update some field of array I see 
the old value. But, if I use the same scheme for ArrayObject I'll get the right 
result.

Test script:
---------------
<?
class t
{
	private $data = array();

	public function __get($name) {
		return $this->data[$name];
	}
	
	public function __set($name, $value) {
		$this->data[$name] = $value;
	}
}

// define test array
$array = array( 'view' => array('x' => 1, 'y' => 1) );

$t = new t();
$t->mas = $array;

// update field of array
$t->mas['view']['x'] = 1111;
echo $t->mas['view']['x'];

echo '<br>';

$t->mas2 = new ArrayObject($array);
$t->mas2['view']['x'] = 1111;
echo $t->mas2['view']['x'];

?>

Expected result:
----------------
1111
<br>
1111

Actual result:
--------------
1
<br>
1111

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-05 11:02 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-08-05 11:02 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

When using the array as storage the __get() method will return a copy of the array. When using ArrayObject it will return a handle to the object. So with an array you're working on a copy, with objects on the same object.

For objet handles check http://www.php.net/manual/en/language.oop5.references.php
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 11:01:33 2025 UTC