Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:8307 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 48249 invoked by uid 1010); 2 Mar 2004 14:08:56 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 48171 invoked by uid 1007); 2 Mar 2004 14:08:56 -0000 Message-ID: <20040302140856.48139.qmail@pb1.pair.com> To: internals@lists.php.net Mail-Copies-To: philip@stutchbury.com Date: Tue, 02 Mar 2004 14:07:11 +0000 Lines: 98 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: Cannot use foreach() with $this->my_iterator within a class From: philip@stutchbury.com (Philip Fletcher) The following code attempts to use a class's iterator member with foreach. This code will not run unless you comment out the offending foreach (marked '** Comment out the next three lines...'). As demonstrated, it is possible to get() the iterator class and run it using foreach() externally or to use it internally with a for() construct, but it is not possible to use a foreach() construct internally when referencing $this->my_iterator member. Apologies for the squished up code: 'ONE' , 'two' => 'TWO' , 'three' => 'THREE' ); protected $position = 0; function rewind() { reset($this->arr); $this->position = 0; } function isValid() { return $this->position < count($this->arr); } function hasMore() { //Only used with foreach() return $this->isValid(); } function hasNext() { return $this->position < count($this->arr)-1; } function key() { return key($this->arr); } function current() { return current($this->arr); } function next() { next($this->arr); $this->position++; } } //end class MyIterator //** Test the iterator $i = new MyIterator(); echo 'Starting iteration for $i...
'; foreach($i as $key => $value) { echo $key .'
'; } echo 'Ended iteration for $i...
'; //** Define a class which has an iterator member class MyClass { var $my_iterator; function __construct($my_iterator) { $this->my_iterator = $my_iterator; } function getMyIterator() { return $this->my_iterator; } function doFor() { echo 'Starting iteration for MyClass->doFor...
'; for( $this->my_iterator->rewind(); $this->my_iterator->hasMore(); //should be isValid() :) $this->my_iterator->next() ) { $key = $this->my_iterator->key(); echo $key . '
'; } echo 'Ended iteration for MyClass->doFor.
'; } function doForEach() { echo 'Starting iteration for MyClass->doForEach...
'; //** Comment the next three lines to make this code run foreach($this->my_iterator as $key => $value) { echo $key . '
'; } echo 'Ended iteration for MyClass->doForEach.
'; } } //end class MyClass //** Instantiate the class $c = new MyClass($i); //** Iterate by getting the iterator - OK echo 'Starting iteration for $c->getMyIterator()...
'; foreach($c->getMyIterator() as $key => $value) { echo $key .'
'; } echo 'Ended iteration for $c->getMyIterator()...
'; //** Make the iterator run within the class (doFor) - SUCCESS $c->doFor(); //** Make the iterator run within the class (doForEach) - FAILURE $c->doForEach(); ?>