note 124944 added to class.cachingiterator
| From: | ahmad dot mayahi at gmail dot com | Date: | Sat, 25 Apr 2020 21:53:22 +0000 |
| Subject: | note 124944 added to class.cachingiterator | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214534@lists.php.net to get a copy of this message | ||
The only difference between CachingIterator and other Iterators such as ArrayIterator is the
hasNext() method.
Since the data will be loaded into the memory, the CachingIterator is able to check whether the
given iterator has a next element.
Let's demonstrate this by an example:
<?php
$iterator = new CachingIterator(new ArrayIterator(['C', 'C++', 'C#',
'PHP', 'Python', 'Go', 'Ruby']));
foreach ($iterator as $item) {
if ($iterator->hasNext()) {
echo $item.', ';
} else {
echo 'and '.$item;
}
}
// C, C++, C#, PHP, Python, Go, and Ruby
?>
In this example I check whether the iterator has a next value, if so, I append a comma otherwise
"and" will be appended to the last element.
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.195 (proxied: 109.57.54.31)
----
Manual Page -- http://php.net/manual/en/class.cachingiterator.php
Edit -- https://master.php.net/note/edit/124944
Del: integrated -- https://master.php.net/note/delete/124944/integrated
Del: useless -- https://master.php.net/note/delete/124944/useless
Del: bad code -- https://master.php.net/note/delete/124944/bad+code
Del: spam -- https://master.php.net/note/delete/124944/spam
Del: non-english -- https://master.php.net/note/delete/124944/non-english
Del: in docs -- https://master.php.net/note/delete/124944/in+docs
Del: other reasons-- https://master.php.net/note/delete/124944
Reject -- https://master.php.net/note/reject/124944
Search -- https://master.php.net/manage/user-notes.php