mirror of
https://github.com/php/php-src.git
synced 2024-10-02 23:36:16 +00:00
c9709bfbd7
Generators are now automatically detected by the presence of a `yield` expression in their body. This removes the ZEND_SUSPEND_AND_RETURN_GENERATOR opcode. Instead additional checks for ZEND_ACC_GENERATOR are added to the fcall_common helper and zend_call_function. This also adds a new function zend_generator_create_zval, which handles the actual creation of the generator zval from an op array. I feel like I should deglobalize the zend_create_execute_data_from_op_array code a bit. It currently changes EG(current_execute_data) and EG(opline_ptr) which is somewhat confusing (given the name).
45 lines
653 B
PHP
45 lines
653 B
PHP
--TEST--
|
|
Generator methods can yield by reference
|
|
--FILE--
|
|
<?php
|
|
|
|
class Test implements IteratorAggregate {
|
|
protected $data;
|
|
|
|
public function __construct(array $data) {
|
|
$this->data = $data;
|
|
}
|
|
|
|
public function getData() {
|
|
return $this->data;
|
|
}
|
|
|
|
public function &getIterator() {
|
|
foreach ($this->data as $key => &$value) {
|
|
yield $key => $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
$test = new Test([1, 2, 3, 4, 5]);
|
|
foreach ($test as &$value) {
|
|
$value *= -1;
|
|
}
|
|
|
|
var_dump($test->getData());
|
|
|
|
?>
|
|
--EXPECT--
|
|
array(5) {
|
|
[0]=>
|
|
int(-1)
|
|
[1]=>
|
|
int(-2)
|
|
[2]=>
|
|
int(-3)
|
|
[3]=>
|
|
int(-4)
|
|
[4]=>
|
|
&int(-5)
|
|
}
|