svn: /phpdoc/en/trunk/language/oop5/ iterations.xml
| From: | Sean Coates | Date: | Wed, 28 Jul 2010 02:59:12 +0000 |
| Subject: | svn: /phpdoc/en/trunk/language/oop5/ iterations.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-6932@lists.php.net to get a copy of this message | ||
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>