FAQ
sean Wed, 28 Jul 2010 02:59:12 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=301637

Log:
fix the iterator implementation. If the array contains a value of false, the previous implementation would bail (the valid() function issues current() which returns false, just as if the array returns false; the new implementation keeps its own index, so there's no need to use current().

Changed paths:
U phpdoc/en/trunk/language/oop5/iterations.xml

Modified: phpdoc/en/trunk/language/oop5/iterations.xml
===================================================================
--- phpdoc/en/trunk/language/oop5/iterations.xml 2010-07-28 02:13:24 UTC (rev 301636)
+++ phpdoc/en/trunk/language/oop5/iterations.xml 2010-07-28 02:59:12 UTC (rev 301637)
@@ -82,6 +82,7 @@
class MyIterator implements Iterator
{
private $var = array();
+ private $index = 0;

public function __construct($array)
{
@@ -90,34 +91,49 @@
}
}

- public function rewind() {
+ public function rewind()
+ {
echo "rewinding\n";
- reset($this->var);
+ $this->index = 0;
}

- public function current() {
- $var = current($this->var);
+ public function current()
+ {
+ $k = array_keys($this->var);
+ $var = $this->var[$k[$this->index]];
echo "current: $var\n";
return $var;
}

- public function key() {
- $var = key($this->var);
+ public function key()
+ {
+ $k = array_keys($this->var);
+ $var = $k[$this->index];
echo "key: $var\n";
return $var;
}

- public function next() {
- $var = next($this->var);
- echo "next: $var\n";
- return $var;
+ public function next()
+ {
+ $k = array_keys($this->var);
+ if (isset($k[++$this->index])) {
+ $var = $this->var[$k[$this->index]];
+ echo "next: $var\n";
+ return $var;
+ } else {
+ echo "next:\n";
+ return false;
+ }
}

- public function valid() {
- $var = $this->current() !== false;
+ public function valid()
+ {
+ $k = array_keys($this->var);
+ $var = isset($k[$this->index]);
echo "valid: {$var}\n";
return $var;
}
+
}

$values = array(1,2,3);
@@ -133,26 +149,22 @@
<screen role="php">
<![CDATA[
rewinding
-current: 1
valid: 1
current: 1
key: 0
0: 1
next: 2
-current: 2
valid: 1
current: 2
key: 1
1: 2
next: 3
-current: 3
valid: 1
current: 3
key: 2
2: 3
-next:
-current:
-valid:
+next: (no more)
+valid:
]]>
</screen>

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 1 | next ›
Discussion Overview
groupphp-doc-cvs @
categoriesphp
postedJul 28, '10 at 2:59a
activeJul 28, '10 at 2:59a
posts1
users1
websitephp.net

1 user in discussion

Sean Coates: 1 post

People

Translate

site design / logo © 2018 Grokbase