note 117108 added to class.iteratoriterator
| From: | sven at rtbg dot de | Date: | Wed, 15 Apr 2015 20:57:02 +0000 |
| Subject: | note 117108 added to class.iteratoriterator | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202276@lists.php.net to get a copy of this message | ||
This iterator basically is only a wrapper around another iterator. It does nothing fancy, it simply
forwards any calls of rewind(), next(), valid(), current() and key() to the inner iterator. This
inner iterator can be fetched with getInnerIterator().
One special case: When passing an IteratorAggregate object, the getIterator() method of that object
will be called and THAT iterator will be iterated over, and this will also be returned when calling
getInnerIterator().
This class can be extended, so it's an ideal building block for your own classes that only want
to modify one or two of the iterator methods, but not all.
Want to trim the strings returned by the current() method?
<?php
class TrimIterator extends IteratorIterator
{
public function current() {
return trim(parent::current());
}
}
$innerIterator = new ArrayIterator(array('normal', ' trimmable '));
$trim = new TrimIterator($innerIterator);
foreach ($trim as $key => $value) {
echo "Key:\n";
var_dump($key);
echo "Value:\n";
var_dump($value);
echo "---next---";
}
?>
Output:
Key:
int(0)
Value:
string(6) "normal"
---next---Key:
int(1)
Value:
string(9) "trimmable"
---next---
----
Server IP: 72.52.91.14
Probable Submitter: 2a02:810d:1b80:8c4:f4fe:fcd4:7f74:fda8
----
Manual Page -- http://php.net/manual/en/class.iteratoriterator.php
Edit -- https://master.php.net/note/edit/117108
Del: integrated -- https://master.php.net/note/delete/117108/integrated
Del: useless -- https://master.php.net/note/delete/117108/useless
Del: bad code -- https://master.php.net/note/delete/117108/bad+code
Del: spam -- https://master.php.net/note/delete/117108/spam
Del: non-english -- https://master.php.net/note/delete/117108/non-english
Del: in docs -- https://master.php.net/note/delete/117108/in+docs
Del: other reasons-- https://master.php.net/note/delete/117108
Reject -- https://master.php.net/note/reject/117108
Search -- https://master.php.net/manage/user-notes.php