|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 21:00:01 2025 UTC |
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()?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.31The 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;