• <legend id='fD12e'><style id='fD12e'><dir id='fD12e'><q id='fD12e'></q></dir></style></legend>

    <i id='fD12e'><tr id='fD12e'><dt id='fD12e'><q id='fD12e'><span id='fD12e'><b id='fD12e'><form id='fD12e'><ins id='fD12e'></ins><ul id='fD12e'></ul><sub id='fD12e'></sub></form><legend id='fD12e'></legend><bdo id='fD12e'><pre id='fD12e'><center id='fD12e'></center></pre></bdo></b><th id='fD12e'></th></span></q></dt></tr></i><div id='fD12e'><tfoot id='fD12e'></tfoot><dl id='fD12e'><fieldset id='fD12e'></fieldset></dl></div>
    • <bdo id='fD12e'></bdo><ul id='fD12e'></ul>

        <tfoot id='fD12e'></tfoot>

        <small id='fD12e'></small><noframes id='fD12e'>

        PHP - 使用迭代器的原因?

        时间:2023-11-30
      1. <tfoot id='jl7r4'></tfoot>

            <small id='jl7r4'></small><noframes id='jl7r4'>

              <tbody id='jl7r4'></tbody>
            1. <legend id='jl7r4'><style id='jl7r4'><dir id='jl7r4'><q id='jl7r4'></q></dir></style></legend>
              <i id='jl7r4'><tr id='jl7r4'><dt id='jl7r4'><q id='jl7r4'><span id='jl7r4'><b id='jl7r4'><form id='jl7r4'><ins id='jl7r4'></ins><ul id='jl7r4'></ul><sub id='jl7r4'></sub></form><legend id='jl7r4'></legend><bdo id='jl7r4'><pre id='jl7r4'><center id='jl7r4'></center></pre></bdo></b><th id='jl7r4'></th></span></q></dt></tr></i><div id='jl7r4'><tfoot id='jl7r4'></tfoot><dl id='jl7r4'><fieldset id='jl7r4'></fieldset></dl></div>
                • <bdo id='jl7r4'></bdo><ul id='jl7r4'></ul>
                  本文介绍了PHP - 使用迭代器的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我今天仔细阅读手册并注意到各种迭代器.在我看来,它们似乎都毫无意义;除非您更喜欢它们的语法,或者不知道如何编写递归函数,否则我看不出使用它们的理由.是否有任何理由在 PHP 中使用内置迭代器而不是编写循环或创建递归方法?

                  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 their syntax, or don't know how to write a recursive function. Are there any reasons to use built-in iterators in PHP over just writing a loop or making a recursive method?

                  我只是在寻找事实的回应,而不是主观的偏好(即:我不在乎你认为它更可读"还是更面向对象",我想知道它们是否更快,提供滚动您自己的等时无法实现的功能.

                  I'm only looking for factual responses, not subjective preferences (ie: I don't care if you think it's more "readable" or "more object-oriented", I want to know if they're faster, offer functionality that can't be achieved when rolling your own, etc.).

                  推荐答案

                  我认为主要原因是多功能性.它们不会使事情更具可读性,您可以完成 spl 迭代器 用一个简单的 foreach 来做.

                  I believe the main reason is versatility. They don't make things more readable and you can accomplish most of what the spl iterators do with a simple foreach.

                  但迭代器可以作为一种压缩循环或循环源,您可以传递它.您可以拥有比列表/数组更多样化的数据源.真正的优势是您可以包装或堆叠它们以进行聚合.

                  But iterators can serve as sort of a compacted loop or loop source that you can pass around. You can have more diverse data sources than just a list/array. And the real advantage is that you can wrap or stack them for aggregation.

                  命名有点不明显,但我想你可以使用 AppendIterator 来例如,结合文件名的静态列表和 DirectoryIterator 的结果.或者以其他方式将其包装在 LimitIterator 中,而不是将其硬接线到 for 条件.所有这些都是非常基本的功能,可以在 foreach 中手动完成.但至少 RegexIteratorFilterIterator 似乎适合降低一些复杂性.

                  The naming is somewhat unobvious, but I guess you could use the AppendIterator to for example combine a static list of filenames and the result of a DirectoryIterator. Or otherwise wrap that in a LimitIterator instead of hardwiring that into a for condition. All of these are pretty basic features and can be done manually in a foreach. But at least RegexIterator and FilterIterator seem suitable to reduce some complexity.

                  但归根结底,这确实是一种风格选择.如果您有遍历某些东西的自定义对象(例如,可以指向真实文件或数据库条目的 VFS 对象),那将是最有意义的.但即便如此,您也可以 iterator_to_array 转换这样的列表,并使用普通的 foreach-over-array.

                  But in the end it's really a stylistic choice. It would make most sense if you have custom objects which traverse something (for example a VFS object which can either point to real files or database entries). But even then you could just iterator_to_array convert such a list, and use a normal foreach-over-array.

                  唯一需要选择迭代器的情况是数据源的大小不受限制.foreach 中使用的展开数组比可以逐个检索元素的迭代器消耗更多的内存.

                  The only case were it is really imperative to prefer iterators were if the data source is unlimited in size. Unfolded arrays for use in a foreach can consume more memory than an iterator which can retrieve element by element.

                  这篇关于PHP - 使用迭代器的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是否可以回退 PDO 结果? 下一篇:可以从这段 PHP 代码中消除 for 循环吗?

                  相关文章

                • <legend id='N8cjI'><style id='N8cjI'><dir id='N8cjI'><q id='N8cjI'></q></dir></style></legend>

                  <small id='N8cjI'></small><noframes id='N8cjI'>

                      <bdo id='N8cjI'></bdo><ul id='N8cjI'></ul>

                      <i id='N8cjI'><tr id='N8cjI'><dt id='N8cjI'><q id='N8cjI'><span id='N8cjI'><b id='N8cjI'><form id='N8cjI'><ins id='N8cjI'></ins><ul id='N8cjI'></ul><sub id='N8cjI'></sub></form><legend id='N8cjI'></legend><bdo id='N8cjI'><pre id='N8cjI'><center id='N8cjI'></center></pre></bdo></b><th id='N8cjI'></th></span></q></dt></tr></i><div id='N8cjI'><tfoot id='N8cjI'></tfoot><dl id='N8cjI'><fieldset id='N8cjI'></fieldset></dl></div>
                      <tfoot id='N8cjI'></tfoot>