PHP 4.0 Bug #8373 Updated: foreach by reference
| From: | cynic@php.net | Date: | Tue, 23 Jan 2001 17:09:21 +0000 |
| Subject: | PHP 4.0 Bug #8373 Updated: foreach by reference | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-44831@lists.php.net to get a copy of this message | ||
ID: 8373
Updated by: cynic
Reported By: ocomte@guarantycity.com
Old-Status: Open
Status: Duplicate
Bug Type: Feature/Change Request
Assigned To:
Comments:
dupe of #3074
btw, +1 for foreach( $a as &$v ) :)
Previous Comments:
---------------------------------------------------------------------------
[2000-12-22 05:47:53] ocomte@guarantycity.com
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).
---------------------------------------------------------------------------
Full Bug description available at: http://bugs.php.net/?id=8373