Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:8174 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 4361 invoked by uid 1010); 26 Feb 2004 16:02:55 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 4325 invoked by uid 1007); 26 Feb 2004 16:02:55 -0000 Message-ID: <20040226160254.4324.qmail@pb1.pair.com> To: internals@lists.php.net Date: Thu, 26 Feb 2004 15:53:50 +0000 Lines: 25 Organization: Stutchbury User-Agent: KNode/0.7.2 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Posted-By: 62.49.23.238 Subject: Iterator Class/foreach Logic Flaw? From: philip@stutchbury.com (Philip Fletcher) At the risk of making a complete fool of myself, I'd like to ask the smarter brains than me to checkout the current logic in the implementation of Iterator classes and foreach(). The current logic of foreach( $obj as $key => $val ) { ... } where $obj implements Iterator, is: for($obj->rewind(); $obj->hasMore(); $obj->next() ) { ... } but it should be: for($obj->rewind(); $obj->isValid(); $obj->next() ) { ... } As the iterator exits the penultimate interation, next() is called, placing the 'pointer' on the last 'record'. But before the last record is processed, hasMore() is called, which *should* return FALSE, because we have already moved to the last record. The implementations I have seen so far all return TRUE from hasMore() when positioned (i.e. current()) on the last record - this is not logical (Captain). It also prevents proper use of a class implementing an iterator outside of the foreach construct. Philip