我应该在PHP中使用哪种Iterator实现,为什么?

时间:2022-04-06 05:22:16

I'm trying to refactor a large, old project and one thing I've noticed is a range of different Iterator implementations:

我正在尝试重构一个大型的旧项目,我注意到的一件事是一系列不同的Iterator实现:

while($iterator->moveNext()) {
    $item = $iterator->current();
    // do something with $item;
}   

for($iterator = getIterator(), $iterator->HasNext()) {
    $item = $iterator->Next();
    // do something with $item
}   

while($item = $iterator->fetch()) {
    // do something with item
}   

or even the StandardPHPLibrary (SPL) iterator which allows

甚至允许的StandardPHPLibrary(SPL)迭代器

foreach($iterator as $item) {
    // do something with $item
}   

Having so many different Iterators (with different methods for looping over collections) seems like a strong code smell, and I'm inclined to refactor everything to SPL. Is there a compelling advantage to any of these implementations of Iterator, or is it purely a matter of personal taste?

有这么多不同的迭代器(使用不同的方法来循环集合)看起来像一个强大的代码味道,我倾向于将所有内容重构为SPL。是否有任何这些Iterator实现的强大优势,还是纯粹是个人品味的问题?

2 个解决方案

#1


9  

The SPL version is definitely the way to go. Not only is it the easiest to read, but it's a part of PHP now, so will be familiar to many more people.

SPL版本绝对是最佳选择。它不仅是最容易阅读的,而且它现在是PHP的一部分,因此对更多人来说会很熟悉。

There's nothing "wrong" with the others, but as you stated, having all these different versions in one project isn't helping anyone.

其他人没有“错误”,但正如你所说,在一个项目中拥有所有这些不同的版本并没有帮助任何人。

#2


1  

Imo, simply utilising one or more of the SPL libraries as an interface tends to be less ugly in use at the front end. However, the backing behind the implementation can get a bit ugly.

Imo,简单地使用一个或多个SPL库作为接口,在前端使用时往往不那么难看。但是,实施背后的支持可能会有点难看。

For instance, I wrote an iterator that efficiently iterated a database result set, so that results that were never requested were never fetched from the request pointer, and if items were prematurely fetched ( IE: $obj[ 5 ] ) , it would seek all the required results into an internal buffer.

例如,我编写了一个有效迭代数据库结果集的迭代器,因此永远不会从请求指针中获取从未请求过的结果,如果过早地获取了项目(IE:$ obj [5]),它会寻求所有将所需结果导入内部缓冲区。

Worked wonderfully, you just pray that code that makes the magic behind the scenes never fails because it confuses people when they see you use something that looks like an array, and it does "magic" which can fail :)

工作得非常好,你只是祈祷那些使幕后魔术永远不会失败的代码,因为当他们看到你使用看起来像数组的东西时会让人感到困惑,并且它会“失败”,它会失败:)

Magic got people burnt at the stake. So use it carefully and wisely, possibly make it obvious how it works.

魔术让人们在火刑柱上被焚烧。因此,请谨慎而明智地使用它,可能会使其显而易见。

My personal preference is for the

我个人的偏好是为了

for( $object as $i => $v ) 

notation for it is generally more consistent and predictable.

它的符号通常更加一致和可预测。

for( $dbresult->iterator() as $i => $v ){ 

}

style notation is functionally identical, but at least you have less guesswork how it works on the surface.

样式符号在功能上是相同的,但至少你猜测它在表面上是如何工作的。

#1


9  

The SPL version is definitely the way to go. Not only is it the easiest to read, but it's a part of PHP now, so will be familiar to many more people.

SPL版本绝对是最佳选择。它不仅是最容易阅读的,而且它现在是PHP的一部分,因此对更多人来说会很熟悉。

There's nothing "wrong" with the others, but as you stated, having all these different versions in one project isn't helping anyone.

其他人没有“错误”,但正如你所说,在一个项目中拥有所有这些不同的版本并没有帮助任何人。

#2


1  

Imo, simply utilising one or more of the SPL libraries as an interface tends to be less ugly in use at the front end. However, the backing behind the implementation can get a bit ugly.

Imo,简单地使用一个或多个SPL库作为接口,在前端使用时往往不那么难看。但是,实施背后的支持可能会有点难看。

For instance, I wrote an iterator that efficiently iterated a database result set, so that results that were never requested were never fetched from the request pointer, and if items were prematurely fetched ( IE: $obj[ 5 ] ) , it would seek all the required results into an internal buffer.

例如,我编写了一个有效迭代数据库结果集的迭代器,因此永远不会从请求指针中获取从未请求过的结果,如果过早地获取了项目(IE:$ obj [5]),它会寻求所有将所需结果导入内部缓冲区。

Worked wonderfully, you just pray that code that makes the magic behind the scenes never fails because it confuses people when they see you use something that looks like an array, and it does "magic" which can fail :)

工作得非常好,你只是祈祷那些使幕后魔术永远不会失败的代码,因为当他们看到你使用看起来像数组的东西时会让人感到困惑,并且它会“失败”,它会失败:)

Magic got people burnt at the stake. So use it carefully and wisely, possibly make it obvious how it works.

魔术让人们在火刑柱上被焚烧。因此,请谨慎而明智地使用它,可能会使其显而易见。

My personal preference is for the

我个人的偏好是为了

for( $object as $i => $v ) 

notation for it is generally more consistent and predictable.

它的符号通常更加一致和可预测。

for( $dbresult->iterator() as $i => $v ){ 

}

style notation is functionally identical, but at least you have less guesswork how it works on the surface.

样式符号在功能上是相同的,但至少你猜测它在表面上是如何工作的。