Bug #75117 [Com]: yield-by-reference in method class
| From: | alditis at gmail dot com | Date: | Sat, 26 Aug 2017 01:06:15 +0000 |
| Subject: | Bug #75117 [Com]: yield-by-reference in method class | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-210823@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75117&edit=1
ID: 75117
Comment by: alditis at gmail dot com
Reported by: alditis at gmail dot com
Summary: yield-by-reference in method class
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Ubuntu 16.04
PHP Version: 7.1.8
Block user comment: N
Private report: N
New Comment:
I added some changes. Now work it's. Thanks for the suggestions:
https://3v4l.org/oY8Iv
Previous Comments:
------------------------------------------------------------------------
[2017-08-25 08:02:28] requinix@php.net
If you want $value to alter $data then you need references at every step between them. That's
three more &s.
Your code got a bit messy after experimenting. Try https://3v4l.org/fiVkl
------------------------------------------------------------------------
[2017-08-25 04:59:20] alditis at gmail dot com
Description:
------------
yield-by-reference in the class method not working I how expected.
Test script:
---------------
class Test
{
public $data = [];
function __construct($data){
$this->data = $data;
}
function &getIterator($d) {
$this->data = $d;
foreach ($d as $key => $value) {
yield $key => $value;
}
}
function printData()
{
foreach ($this->data as $key => $value) {
echo($key . ':' . $value . PHP_EOL);
}
}
}
$data = array('one'=>'Curly', 'two'=>'Larry',
'three'=>'Moe');
$t = new Test($data);
foreach ($t->getIterator($data) as $key => &$value) {
$value = strtoupper($value);
}
$t->printData();
Expected result:
----------------
I expected names in uppercase:
one:CURLY
two:LARRY
three:MOE
Actual result:
--------------
$t->printData() of the test script display:
one:Curly
two:Larry
three:Moe
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75117&edit=1