ID: 38102
Comment by: pstradomski at gmail dot com
Reported By: frode at coretrek dot com
Status: Open
Bug Type: Documentation problem
Operating System: Linux
PHP Version: 5CVS-2006-07-14 (snap)
New Comment:
The problem still exists, albeit in minor version. Trying to iterate
over an array returned by __get with foreach construct yields a false
E_NOTICE : "Indirect modification of overloaded property
myObject::$array has no effect".
Test case:
<?php
class A {
public function __get($val) {
return $this->keys[$val];
}
public function __set($k, $v) {
$this->keys[$k] = $v;
}
private $keys = array();
}
$a =new A();
$a->arr = array('a','b','c');
foreach ($a->arr as $k => $v) {
echo "$k => $v\n";
}
?>
Expected result: no error message
Actual result: E_NOTICE error.
Previous Comments:
------------------------------------------------------------------------
[2006-08-15 07:22:53] frode at coretrek dot com
Thank you for your quick and consise answer. Yet again, I re-open this
bug because I now think this is a documentation issue.
I looked through the documentation on overloading
(http://www.php.net/manual/en/language.oop5.overloading.php) and could
not find any mention of return-by-value and return-by-reference.
I think it would be very helpful to add a note to the manual stating
something about __get()/__set() only works by-value.
Also, this means there is no way to make a generic
"shadow"/"call-through"/"wrapper" class for a class that exposes public
non-object members, which is a shame (should I open a feature request
bug for this?). This should perhaps also be noted in the manual, along
with __call() also returning by-value?
For reference, old bug summary was: Assigning and updating array
through __get()/__set() no longer works in 5.2-snap
------------------------------------------------------------------------
[2006-08-14 10:53:48] [EMAIL PROTECTED]
Because __get() returns variables by value, not by reference.
It's identical to this code:
<?php
$var = 1;
intval($var) = 5; //you wouldn't expect it to work, would you?
?>
------------------------------------------------------------------------
[2006-08-14 10:47:23] frode at coretrek dot com
I'm taking the liberty to re-open the bug, since there has been no
explanation for why 5.2 breaks code that works in 5.1.4.
Thanks in advance to anyone that can clarify up this issue.
------------------------------------------------------------------------
[2006-07-14 10:05:17] frode at coretrek dot com
Yes, I did run php5.2-200607140630.
The error message you pasted is identical with the one I entered in
'actual results' in the original submission of this bug.
Is this is a planned language change for PHP 5.2? Because the code runs
with no errors in PHP 5.1.4.
------------------------------------------------------------------------
[2006-07-14 09:37:41] [EMAIL PROTECTED]
Did you REALLY try the latest snapshot?
This is what I get with your code:
Fatal error: Cannot use array returned from Class1::__get('property')
in write context ...
And this is well expected.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/38102
--
Edit this bug report at http://bugs.php.net/?id=38102&edit=1