offsetExists($index) 檢查的是快取,而不是內部或外部迭代器。
<?php
$cache = new \CachingIterator(
new \ArrayIterator(['a', 'b', 'c', 'd']),
\CachingIterator::FULL_CACHE);
$shortRange = range(0, 1);
$fullRange = range(0, 3);
foreach ($shortRange as $index) {
$cache->next();
}
echo PHP_EOL . '快取' . PHP_EOL;
var_export($cache->getCache());
echo PHP_EOL;
foreach ($fullRange as $offset) {
print_r("快取偏移 '$offset' " .
($cache->offsetExists("$offset") == 1
? '存在'
: "不存在"
) . PHP_EOL);
}
?>
快取
array (
0 => 'a',
1 => 'b',
)
快取偏移 '0' 存在
快取偏移 '1' 存在
快取偏移 '2' 不存在
快取偏移 '3' 不存在