Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

关于php yield的一些疑问

php新增了yield,据说能大幅减少内存的占用,但是工作多年,一直没有用过它,主要是感觉没有使用的场景,我看网上有下面的使用示例,感觉这样似乎并不好,下面这个似乎会不停的去数据库取数据,请精通 yield 的大神分析一下,具体哪些场景适合使用这个功能呢?谢谢。

function get(){
    $sql = "select * from `user` limit 0,500000000";
    $stat = $pdo->query($sql);
    while ($row = $stat->fetch()) {
        yield $row;//逐行读出数据库行
    }
}
 
foreach (get() as $row) {
    var_dump($row);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

就这里而言,你用不用 yield 效果都是一样的 ??


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...