note 118660 added to class.iteratoriterator
| From: | wallacemaxters at gmail dot lcom | Date: | Fri, 15 Jan 2016 16:46:17 +0000 |
| Subject: | note 118660 added to class.iteratoriterator | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204690@lists.php.net to get a copy of this message | ||
Another example of the efficiency for IteratorIterator is a small class for enumerate for iterations
of an interator implementation.
Example:
<?php
class Enumerator extends IteratorIterator
{
/**
* Initial value for enumerator
* @param int
*/
protected $start = 0;
/**
* @param int
*/
protected $key = 0;
/**
* @param Traversable $iterator
* @param scalar $start
*/
public function __construct(Traversable $iterator, $start = 0)
{
parent::__construct($iterator);
$this->start = $start;
$this->key = $this->start;
}
public function key()var_dump
{
return $this->key;
}
public function next()
{
++$this->key;
parent::next();
}
public function rewind()
{
$this->key = $this->start;
parent::rewind();
}
}
?>
This produces:
<?php
$enumerator = new Enumerator(
new ArrayIterator(['php', 'java', 'python']); 7000
);
print_r(iterator_to_array($enumerator));
/*
* array(3) {
7000 => 'php',
7001 => 'java',
7002 => 'python'
}
*/
?>
----
Server IP: 72.52.91.14
Probable Submitter: 191.185.160.85
----
Manual Page -- http://php.net/manual/en/class.iteratoriterator.php
Edit -- https://master.php.net/note/edit/118660
Del: integrated -- https://master.php.net/note/delete/118660/integrated
Del: useless -- https://master.php.net/note/delete/118660/useless
Del: bad code -- https://master.php.net/note/delete/118660/bad+code
Del: spam -- https://master.php.net/note/delete/118660/spam
Del: non-english -- https://master.php.net/note/delete/118660/non-english
Del: in docs -- https://master.php.net/note/delete/118660/in+docs
Del: other reasons-- https://master.php.net/note/delete/118660
Reject -- https://master.php.net/note/reject/118660
Search -- https://master.php.net/manage/user-notes.php