|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-01-26 02:16 UTC] adam at trachtenberg dot com
Description:
------------
When a class implements SeekableIterator and provides a
seek() method, SPL does not seek correctly. When SPL is
forced to emulate seek() by advancing step-by-step, it
works correctly. (See NumericArrayIterator vs
SeekableNumericArrayIterator.)
At first, I thought spl_limit_it_seek() needed to call:
if (spl_dual_it_has_more(intern TSRMLS_CC) == SUCCESS) {
spl_dual_it_fetch(intern, 1 TSRMLS_CC);
}
Even for instances of SeekableIterator, but that only
worked with the offset was 0.
Reproduce code:
---------------
class NumericArrayIterator implements Iterator {
protected $a;
protected $i;
public function __construct($a) {
$this->a = $a;
}
public function rewind() {
$this->i = 0;
}
public function hasMore() {
return $this->i < count($this->a);
}
public function key() {
return $this->i;
}
public function current() {
return $this->a[$this->i];
}
public function next() {
$this->i++;
}
}
class SeekableNumericArrayIterator extends NumericArrayIterator implements SeekableIterator {
public function seek($index) {
if ($index < count($this->a)) {
$this->i = $index;
}
}
}
$a = array(1, 2, 3, 4, 5);
foreach (new LimitIterator(new NumericArrayIterator($a), 0, -1) as $v) {
print "$v\n";
}
foreach (new LimitIterator(new SeekableNumericArrayIterator($a), 0, -1) as $v) {
print "$v\n";
}
Expected result:
----------------
1
2
3
4
5
1
2
3
4
5
Actual result:
--------------
1
2
3
4
5
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 00:00:01 2025 UTC |
Thanks for the speedy bug fix. However, I found another problem when you seek() to an amount other than -1. In those cases, you only get 1 result. For example, using the same code as before, but with "3" instead of "-1": $a = array(1, 2, 3, 4, 5); foreach (new LimitIterator(new NumericArrayIterator($a), 0, 3) as $v) { print "$v\n"; } foreach (new LimitIterator(new SeekableNumericArrayIterator($a), 0, 3) as $v) { print "$v\n"; } 1 2 3 1