PHP YIELD使读取大文件变成可能
例子:摘自PHP手册
for the protection from the leaking of resources
see RFC https://wiki.php.net/rfc/generators#closing_a_generatorand use finnaly
sample code
function getLines($file) {
$f = fopen($file, "r");
try {
while ($line = fgets($f)) {
yield $line;
}
} finally {
fclose($f);
}
}
foreach (getLines("file.txt") as $n => $line) {
if ($n > 5) break;
echo $line;
}
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇: YII2 学习笔记-- RESTFUL API