From ca6478c611c9a702eca7063d28c960b4cead5d49 Mon Sep 17 00:00:00 2001 From: David Tajchreber Date: Thu, 30 Dec 2010 10:10:51 +0000 Subject: [PATCH] Patch submitted by anonymous person via online editor git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@306851 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/oop5/iterations.xml | 41 ++++++++++++++---------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/language/oop5/iterations.xml b/language/oop5/iterations.xml index e67162552b..7bc42729c1 100644 --- a/language/oop5/iterations.xml +++ b/language/oop5/iterations.xml @@ -82,7 +82,6 @@ private => private var class MyIterator implements Iterator { private $var = array(); - private $index = 0; public function __construct($array) { @@ -94,43 +93,35 @@ class MyIterator implements Iterator public function rewind() { echo "rewinding\n"; - $this->index = 0; + reset($this->var); } - + public function current() { - $k = array_keys($this->var); - $var = $this->var[$k[$this->index]]; + $var = current($this->var); echo "current: $var\n"; return $var; } - - public function key() + + public function key() { - $k = array_keys($this->var); - $var = $k[$this->index]; + $var = key($this->var); echo "key: $var\n"; return $var; } - - public function next() + + 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; - } + $var = next($this->var); + echo "next: $var\n"; + return $var; } - + public function valid() { - $k = array_keys($this->var); - $var = isset($k[$this->index]); - echo "valid: {$var}\n"; + $key = key($this->var); + $var = ($key !== NULL && $key !== FALSE); + echo "valid: $var\n"; return $var; } @@ -163,7 +154,7 @@ valid: 1 current: 3 key: 2 2: 3 -next: (no more) +next: valid: ]]>