PHP - Reasons to use Iterators?

后端 未结 5 496
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 04:43

I was perusing the manual today and noticed the various iterators. To me, it seems like they are all somewhat pointless; I don\'t see a reason to use them unless you prefer

5条回答
  •  被撕碎了的回忆
    2020-12-08 05:21

    The question can have very wide variety of answers, but proper answers probably can be summarized as: Iterator can be used for iterating anything more complex than simple array.

    One example is some kind of ORM or query builder. Assume we have one - using it could look like that:

    $friends = new Model_Friend();
    $friends = $people->where('age','>',30)->where('gender','=','female');
    foreach ($friends as $friend) {
        echo $friend->name;
    }
    

提交回复
热议问题