From: stochnagara at hotmail dot com
Operating system:
PHP version: 5CVS-2005-09-11 (snap)
PHP Bug Type: Class/Object related
Bug description: foreach + __get + __set incosistency
Description:
------------
There is some incostistency with foreach and a class which has __get and
__set methods. I don't know what is the intended behaviour but there is a
problem there when assigning a foreach key or value to an overloaded
member of such class.
See comments in the expected result region.
Reproduce code:
---------------
<? class abc {
function __set ($key, $value) { echo "__set ($key,$value)<br/>";
$this->arr[$key] = $value; }
function /*&*/ __get ($key) { echo "__get ($key)<br/>"; return
$this->arr[$key]; }
function __isset ($key) { echo "__isset ($key)<br/>"; return isset
($this->arr[$key]); }
function __unset ($key) { echo "__unset ($key)<br/>"; unset
($this->arr[$key]); }
private $arr;
}
$abc = new abc();
foreach (array (1,2,3) as $abc->k => $v) {
print_r($abc);echo ';'; var_dump($abc->k);echo ';';
}
$abc->k = 4;
echo '-----<br/>';
foreach (array (1,2,3) as $abc->k => $v) {
print_r($abc);echo ';'; var_dump($abc->k);echo ';';
}
echo '<br/>-----<br/>';
$abc_value = new abc();
foreach (array (1,2,3) as $v => $abc_value->k) {
print_r($abc_value);echo ';'; var_dump($abc_value->k);echo ';';
}
Expected result:
----------------
Depends of specification.
Case 1 : First foreach fills $arr with keys. Others are ok.
Case 2 : Second foreach does not fill $arr with keys.
Note 1!
When __get is changed to return by reference, first foreach behaves
exactly like the second one.
Note 2!
Third foreach calls __set while first and second do not.
Actual result:
--------------
__get (k)
abc Object ( [arr:private] => Array ( ) ) ;__get (k)
NULL ;__get (k)
abc Object ( [arr:private] => Array ( ) ) ;__get (k)
NULL ;__set (k,4)
-----
__get (k)
abc Object ( [arr:private] => Array ( [k] => 0 ) ) ;__get (k)
int(0) ;__get (k)
abc Object ( [arr:private] => Array ( [k] => 1 ) ) ;__get (k)
int(1) ;
-----
__set (k,1)
abc Object ( [arr:private] => Array ( [k] => 1 ) ) ;__get (k)
int(1) ;__set (k,2)
abc Object ( [arr:private] => Array ( [k] => 2 ) ) ;__get (k)
int(2) ;
--
Edit bug report at http://bugs.php.net/?id=34467&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=34467&r=trysnapshot4
Try a CVS snapshot (php5.0):
http://bugs.php.net/fix.php?id=34467&r=trysnapshot50
Try a CVS snapshot (php5.1):
http://bugs.php.net/fix.php?id=34467&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=34467&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=34467&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=34467&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=34467&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=34467&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=34467&r=support
Expected behavior: http://bugs.php.net/fix.php?id=34467&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=34467&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=34467&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=34467&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=34467&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=34467&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=34467&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=34467&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=34467&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=34467&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=34467&r=mysqlcfg