Bug #66256 [NEW]: Generator::current() does not support return value by reference
| From: | edmond at inbox dot ru | Date: | Tue, 10 Dec 2013 17:33:16 +0000 |
| Subject: | Bug #66256 [NEW]: Generator::current() does not support return value by reference | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-183228@lists.php.net to get a copy of this message | ||
From: edmond at inbox dot ru
Operating system: Windows 7 64
PHP version: 5.5Git-2013-12-10 (snap)
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Generator::current() does not support return value by reference
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 bug report at https://bugs.php.net/bug.php?id=66256&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=66256&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=66256&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=66256&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=66256&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=66256&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=66256&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=66256&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=66256&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=66256&r=support
Expected behavior: https://bugs.php.net/fix.php?id=66256&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=66256&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=66256&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=66256&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66256&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=66256&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=66256&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=66256&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66256&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=66256&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=66256&r=mysqlcfg