我复制了PHP手册的代码:
http://php.net/manual/en/language.generators.syntax.php#example-295
function gen_one_to_three() {
for ($i = 1; $i <= 3; $i++) {
// Note that $i is preserved between yields.
yield $i;
}
}
$generator = gen_one_to_three();
foreach ($generator as $value) {
echo "$value\n";
}
但是当我在浏览器中运行此代码时,它会导致无限循环:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 448 bytes)
在xdebug通知我脚本的内存已耗尽之前。
我在 MAMP 上安装了 PHP 5.6.2,为什么会遇到此错误?
最佳答案
我发现了这个问题,因为所有像我一样使用 PHP AOP 扩展的人 https://github.com/AOP-PHP/AOP :
生成器无法工作,我在 GitHub 上打开了一个问题 https://github.com/AOP-PHP/AOP/issues/93
在我的 php.ini
中,我更改了这一点:
extension=aop.so
对此:
;extension=aop.so
重新启动 Apache,现在一切都按照手册所述完美运行。所以我猜想扩展程序存在错误。
关于php - 生成器函数在 PHP 中不起作用并导致无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29327272/