PHP 4.0 Bug #8373: foreach by reference
| From: | ocomte at guarantycity dot com | Date: | Fri, 22 Dec 2000 10:47:53 +0000 |
| Subject: | PHP 4.0 Bug #8373: foreach by reference | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-42116@lists.php.net to get a copy of this message | ||
From: ocomte@guarantycity.com
Operating system: All
PHP version: 4.0.4
PHP Bug Type: Feature/Change Request
Bug description: foreach by reference
It would be very useful (at least for me) if the fonction foreach support references. For example
the code :
<?
class Foo
{ var $Ok=false;
function EchoOk()
{ if ($this->Ok)
echo "OK<br>\n";
else
echo "KO<br>\n";
}
}
$Bar=array();
$Bar[]=new Foo;
foreach($Bar as $v)
$v->SetOk();
foreach($Bar as $v)
$v->EchoOk();
?>
will produce the output :
KO<br>
I know I can replace the first loop by :
foreach($Bar as $k => $v)
$Bar[$k]->SetOk();
but, in this case, the $v is useless.
I suggest to accept the notation
foreach($Arr as &$RefVal)
and
foreach($Arr as $Key => &$RefVal)
in a future PHP release to make $RefVal a reference on each $Arr elements (and not a copy).
--
Edit Bug report at: http://bugs.php.net/?id=8373&edit=1