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

Categories

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.9k views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...