#38478 [Asn->Bgs]: reset() does not work as expected on Iterator implementations

From: Date: Tue, 31 Oct 2006 23:32:18 +0000
Subject: #38478 [Asn->Bgs]: reset() does not work as expected on Iterator implementations
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-104240@lists.php.net to get a copy of this message
ID: 38478 Updated by: helly@php.net Reported By: toby@php.net -Status: Assigned +Status: Bogus Bug Type: SPL related -Operating System: Gentoo +Operating System: * -PHP Version: 5.2.0RC1 +PHP Version: 5.2.0 Assigned To: helly New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php In PHP objects are also arrays of their properties and reset and friends only work on real arrays. Thus the result is correct. Previous Comments: ------------------------------------------------------------------------ [2006-08-17 08:20:07] toby@php.net Description: ------------ The Iterator interface (SPL) defines a method rewind(). When implementing e.g. ArrayIterator to allow array access to an object, one would expect that reset($object) would use the rewind method. This does not happen, but the first property is returned. Reproduce code: --------------- <?php class testArray implements Iterator { protected $prop = array( 1 => 'one', 2 => 'two', 3 => 'three', ); public function rewind() { return reset( $this->prop ); } public function key() { return key( $this->prop ); } public function current() { return current( $this->prop ); } public function next() { return next( $this->prop ); } public function valid() { return ( current( $this->prop ) !== false ); } } $array = new testArray(); Expected result: ---------------- // Expected: string(3) "one" var_dump( $array->rewind() ); // Expected: string(3) "one" var_dump( reset( $array ) ); Actual result: -------------- // As expected: string(3) "one" var_dump( $array->rewind() ); // Expected: string(3) "one" var_dump( reset( $array ) ); /* Got: array(3) { [1]=> string(3) "one" [2]=> string(3) "two" [3]=> string(5) "three" } */ ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=38478&edit=1

« previous php.bugs (#104240) next »