note 124513 added to class.iterator

From: Date: Mon, 16 Dec 2019 15:57:34 +0000
Subject: note 124513 added to class.iterator
Groups: php.notes 
Request: Send a blank email to php-notes+get-213544@lists.php.net to get a copy of this message
RocketInABog's seemingly trivial tIterator_array class has one huge problem (which just cost me a couple of hours). Consider this example, using their class: <?php $values = ['one', 'two', 'three']; foreach ($values as $v) {} $current = current($values); // $current === 'one', as you would expect $iterator = new tIterator_array($values); foreach ($iterator as $v) {} $current = $iterator->current(); // do NOT use current($iterator) or key($iterator)!!! // $current === false, but why? ?> The problem is that foreach resets arrays, but doesn't call Iterator::rewind on objects! I also think it's a design mistake that foreach works with Iterator, but current(), key() and end() don't - these iterate over the objects fields. I just refactored some code to use an Iterator instead of an array, and it broke in several very unexpected ways because of these differences. ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.195 (proxied: 2001:985:c7aa:1:fd9e:70c6:a697:c33b) ---- Manual Page -- http://php.net/manual/en/class.iterator.php Edit -- https://master.php.net/note/edit/124513 Del: integrated -- https://master.php.net/note/delete/124513/integrated Del: useless -- https://master.php.net/note/delete/124513/useless Del: bad code -- https://master.php.net/note/delete/124513/bad+code Del: spam -- https://master.php.net/note/delete/124513/spam Del: non-english -- https://master.php.net/note/delete/124513/non-english Del: in docs -- https://master.php.net/note/delete/124513/in+docs Del: other reasons-- https://master.php.net/note/delete/124513 Reject -- https://master.php.net/note/reject/124513 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#213544) next »