Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:8183 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 63729 invoked by uid 1010); 26 Feb 2004 19:47:22 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 63699 invoked from network); 26 Feb 2004 19:47:22 -0000 Received: from unknown (HELO shiva.mind.de) (212.42.230.204) by pb1.pair.com with SMTP; 26 Feb 2004 19:47:22 -0000 Received: from [192.168.1.105] (pD95F8104.dip.t-dialin.net [217.95.129.4]) by shiva.mind.de (Postfix) with ESMTP id D6BFA97B59; Thu, 26 Feb 2004 20:47:15 +0100 (CET) Date: Thu, 26 Feb 2004 20:45:16 +0100 Reply-To: Marcus Boerger X-Priority: 3 (Normal) Message-ID: <1051368455281.20040226204516@marcus-boerger.de> To: Philip Fletcher Cc: internals@lists.php.net In-Reply-To: <20040226160254.4324.qmail@pb1.pair.com> References: <20040226160254.4324.qmail@pb1.pair.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Iterator Class/foreach Logic Flaw? From: helly@php.net (Marcus Boerger) Hello Philip, have a look at it's dokumentation: ext/spl/spl.php there you'll find that hasMore() needs to be checked before an access to key() or current() can be done. Hence it's meaning is somewhat like isValid(). regards marcus Thursday, February 26, 2004, 4:53:50 PM, you wrote: > 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 -- Best regards, Marcus mailto:helly@php.net