php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40823 Overloaded Array Access Breaks in 5.2.1
Submitted: 2007-03-15 17:17 UTC Modified: 2007-03-15 17:26 UTC
From: php at michaelho dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.1 OS: Mac OS X 10.4.9
Private report: No CVE-ID: None
 [2007-03-15 17:17 UTC] php at michaelho dot com
Description:
------------
This was mistakenly reported as "bogus" in http://bugs.php.net/bug.php?id=40625

Overloaded array access was implemented then broken, then fixed again in PHP 5.2.0: http://bugs.php.net/39449

However, with PHP 5.2.1, it was broken once again.

The following code sample works perfectly in 5.2.0, but is broken in 5.2.1

Reproduce code:
---------------
<?php
	class Foo {
		private $array = array(1, 2, 3);
		public function __get($name) {
			return (array) $this->array;
		}
	}

	$a = new foo();
	$a->overloaded[] = 4;
	foreach ($a->overloaded as $int)
		print $int . "<br/>";
?>

Expected result:
----------------
1
2
3
4

Actual result:
--------------
Notice: Indirect modification of overloaded property Foo::$overloaded has no effect in /deb/blah/wwwroot/test.php on line 10
1
2
3

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-15 17:26 UTC] tony2001@php.net
Please read what Derick said in bug #40625.
It DID NOT work before and the only difference is the notice.

To make it work you need to return __get() result by reference:
<?php
class Foo {
		private $array = array(1, 2, 3);
		public function &__get($name) { //<------- &
			return (array) $this->array;
		}
	}

	$a = new foo();
	$a->overloaded[] = 4;
	foreach ($a->overloaded as $int)
		print $int . "<br/>";
?>

And that's really easy to explain - you can't change the result of the __get() method and expect it to affect the object members. 
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 02 08:01:31 2024 UTC