Generator/send-Phpdoc专题
Generator::send
向生成器中传入一个值
说明
public mixed
Generator::send ( <span
class="methodparam">mixed $value )
向生成器中传入一个值,并且当做 yield 表达式的结果,然后继续执行生成器。
如果当这个方法被调用时,生成器不在 yield 表达式,那么在传入值之前,它会先运行到第一个 yield 表达式。As such it is not necessary to "prime" PHP generators with a Generator::next call (like it is done in Python).
参数
value
传入生成器的值。这个值将会被作为生成器当前所在的
yield
的返回值
返回值
返回生成的值。
范例
示例 #1 用 Generator::send 向生成器函数中传值
<?php
function printer() {
while (true) {
$string = yield;
echo $string;
}
}
$printer = printer();
$printer->send('Hello world!');
?>
以上例程会输出:
Hello world!