mirror of
https://github.com/php/php-src.git
synced 2025-08-15 21:48:51 +02:00
Add $generator->close() method
Calling $generator->close() is equivalent to executing a return statement at the current position in the generator.
This commit is contained in:
parent
12e928314f
commit
72a91d08e7
2 changed files with 49 additions and 0 deletions
32
Zend/tests/generators/generator_close.phpt
Normal file
32
Zend/tests/generators/generator_close.phpt
Normal file
|
@ -0,0 +1,32 @@
|
|||
--TEST--
|
||||
Generator can be closed by calling ->close()
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
function *allNumbers() {
|
||||
for ($i = 0; true; ++$i) {
|
||||
yield $i;
|
||||
}
|
||||
}
|
||||
|
||||
$numbers = allNumbers();
|
||||
|
||||
foreach ($numbers as $n) {
|
||||
var_dump($n);
|
||||
if ($n == 9) {
|
||||
$numbers->close();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
int(0)
|
||||
int(1)
|
||||
int(2)
|
||||
int(3)
|
||||
int(4)
|
||||
int(5)
|
||||
int(6)
|
||||
int(7)
|
||||
int(8)
|
||||
int(9)
|
|
@ -324,6 +324,22 @@ ZEND_METHOD(Generator, send)
|
|||
zend_generator_resume(object, generator TSRMLS_CC);
|
||||
}
|
||||
|
||||
/* {{{ proto void Generator::close()
|
||||
* Closes the generator */
|
||||
ZEND_METHOD(Generator, close)
|
||||
{
|
||||
zend_generator *generator;
|
||||
|
||||
if (zend_parse_parameters_none() == FAILURE) {
|
||||
return;
|
||||
}
|
||||
|
||||
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
|
||||
|
||||
zend_generator_close(generator, 0);
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
ZEND_BEGIN_ARG_INFO(arginfo_generator_void, 0)
|
||||
ZEND_END_ARG_INFO()
|
||||
|
||||
|
@ -338,6 +354,7 @@ static const zend_function_entry generator_functions[] = {
|
|||
ZEND_ME(Generator, key, arginfo_generator_void, ZEND_ACC_PUBLIC)
|
||||
ZEND_ME(Generator, next, arginfo_generator_void, ZEND_ACC_PUBLIC)
|
||||
ZEND_ME(Generator, send, arginfo_generator_send, ZEND_ACC_PUBLIC)
|
||||
ZEND_ME(Generator, close, arginfo_generator_void, ZEND_ACC_PUBLIC)
|
||||
ZEND_FE_END
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue