生成器类
目录
- Generator::current — 返回当前产生的值
- Generator::getReturn — 获取生成器的返回值
- Generator::key — 返回当前产生的键
- Generator::next — 生成器继续执行
- Generator::rewind — 重置迭代器
- Generator::send — 向生成器中传入一个值
- Generator::throw — 向生成器中抛入一个异常
- Generator::valid — 检查迭代器是否被关闭
- Generator::__wakeup — 序列化回调

User Contributed Notes 1 note
Pistachio ¶
6 years ago
Unlike return, yield can be used anywhere within a function so logic can flow more naturally. Take for example the following Fibonacci generator:
<?php
function fib($n)
{
$cur = 1;
$prev = 0;
for ($i = 0; $i < $n; $i++) {
yield $cur;
$temp = $cur;
$cur = $prev + $cur;
$prev = $temp;
}
}
$fibs = fib(9);
foreach ($fibs as $fib) {
echo " " . $fib;
}
// prints: 1 1 2 3 5 8 13 21 34