Bug #66256 [Com]: Generator::current() does not support return value by reference
| From: | elliot at aanet dot com dot au | Date: | Fri, 08 Aug 2014 11:04:27 +0000 |
| Subject: | Bug #66256 [Com]: Generator::current() does not support return value by reference | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-187021@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66256&edit=1
ID: 66256
Comment by: elliot at aanet dot com dot au
Reported by: edmond at inbox dot ru
Summary: Generator::current() does not support return value
by reference
Status: Assigned
Type: Bug
Package: Scripting Engine problem
Operating System: Windows 7 64
PHP Version: 5.5Git-2013-12-10 (snap)
Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
I assume this was never fixed.
Is there any known workaround?
Previous Comments:
------------------------------------------------------------------------
[2013-12-10 17:33:15] edmond at inbox dot ru
Description:
------------
Generator::current() does not support return value by reference.
I note foreach operator works fine with yield result which passed by reference.
The code below works normal:
function &my_test(&$array)
{
foreach($array as $key => &$value)
{
yield $key => $value;
}
}
$array = ['x1' => 1,'x2' => 2, 'x3' => 3,'x4' =>
4];
foreach(my_test($array) as $key => &$value)
{
$value = $value + 10;
echo "$key => $value\n";
}
print_r($array);
But when I use Generator::current() I get a different result.
On the other hand Generator::current() does not have to return value by reference. But there must be
another way to do it.
Test script:
---------------
function &my_test(&$array)
{
foreach($array as $key => &$value)
{
yield $key => $value;
}
}
$array = ['x1' => 1,'x2' => 2, 'x3' =>
3,'x4' => 4];
$generator = my_test($array);
while($generator->valid())
{
$value = $generator->current();
$key = $generator->key();
$value = $value + 10;
echo "$key => $value\n";
$generator->next();
}
print_r($array);
Expected result:
----------------
x1 => 11
x2 => 12
x3 => 13
x4 => 14
Array
(
[x1] => 11
[x2] => 12
[x3] => 13
[x4] => 14
)
Actual result:
--------------
x1 => 11
x2 => 12
x3 => 13
x4 => 14
Array
(
[x1] => 1
[x2] => 2
[x3] => 3
[x4] => 4
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66256&edit=1