php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29378 foreach() doesn't work with arrays returned by __get
Submitted: 2004-07-25 17:47 UTC Modified: 2005-03-19 08:38 UTC
Votes:9
Avg. Score:5.0 ± 0.0
Reproduced:9 of 9 (100.0%)
Same Version:1 (11.1%)
Same OS:4 (44.4%)
From: chernyshevsky at hotmail dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.0.0 OS: Windows 2000
Private report: No CVE-ID: None
 [2004-07-25 17:47 UTC] chernyshevsky at hotmail dot com
Description:
------------
Not sure if this is a duplicate of #28444. It's certainly related.

Basically I was trying to loop through an array set earlier using __set and retrieve through __get. Simple enough it seems, but the engine died with an "Cannot access undefined property for object with overloaded property access" fatal error.

Reproduce code:
---------------
class Object {
	private $values;

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

$O = new Object;
$O->cows = array("Betty", "Agnes", "Jeff");
foreach($O->cows as $cow) {
	echo "<div>$cow</div>";
}

Expected result:
----------------
Betty
Agnes
Jeff

Actual result:
--------------
Fatal error: Cannot access undefined property for object with overloaded property access

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-26 16:45 UTC] cysgwr_eryri at yahoo dot co dot uk
Your 'Object' object doesn't have a 'cows' member variable. The "object with overloaded property access" that the warning is telling you about doesn't exist is this:

$O->cows

Which 'cows' are you referring to?
 [2004-07-28 01:02 UTC] chernyshevsky at hotmail dot com
I know the class doesn't have a member called 'cows'. That's what __get() and __set() is for after all. If I change the code to

$O->cows = array("Betty", "Agnes", "Jeff");
$temp = $O->cows;
foreach($temp as $cow) {
	echo "<div>$cow</div>";
}

Now why should it fail just because the property access is through foreach()?
 [2004-10-25 23:14 UTC] benneh at gmail dot com
I've just run into this problem, here is my test case

Testcase:
---------
class Language {
	private $lang_bits = array(); 
	public function __get($lang_bit) {
		global $$lang_bit;

		if(!isset($this->lang_bits[$lang_bit])) {
			$this->lang_bits[$lang_bit] = $$lang_bit;
		}
		return $this->lang_bits[$lang_bit];
	}
}

$page = array('bla' => 'xyz');
$lang = new Language;
foreach($lang->page as $lang_key => $lang_bit) {
	echo $lang_key.' => '.$lang_bit;
}

Expected Results :
------------------
bla => xyz

Actual Results :
----------------
Fatal error: Cannot access undefined property for object with overloaded property access
Just confirming its presence in 5.0.2 WinXP SP2 Apache 1.3.31
 [2005-02-11 15:42 UTC] l0cky at jolt dot co dot uk
The simplest solution I have is

//Copy works
foreach($array = $object->array as $element)
{
    print($element);
}

However, if you want to access the array elements by reference instead of copy, then you're in trouble.

Even this produces the same error:

$array_ref =& $object->array;
 [2005-03-06 20:52 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-03-14 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2005-03-18 17:38 UTC] jrhernandez05 at gmail dot com
I have tried the original reproduce code using the latest PHP snapshot, and have also tried it using some similar code I'm using in my application, and it's working fine now! My environment is Windows 2000 using Apache2.
 [2005-03-19 08:38 UTC] tony2001@php.net
Ok, marking it as closed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Aug 15 15:01:27 2024 UTC