Bug #79404 [Opn]: Referencing to SplFixedArray interrupts foreach loop

From: Date: Mon, 23 Mar 2020 11:03:27 +0000
Subject: Bug #79404 [Opn]: Referencing to SplFixedArray interrupts foreach loop
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226231@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79404&edit=1 ID: 79404 Updated by: cmb@php.net Reported by: vlad dot turchinskiy at yandex dot ru Summary: Referencing to SplFixedArray interrupts foreach loop Status: Open Type: Bug Package: SPL related Operating System: all PHP Version: 5.4.0 - 7.4.4 Block user comment: N Private report: N New Comment: Yes, a single foreach loop works certainly as expected; the problem is *nesting* of foreach loops: <?php $spl = SplFixedArray::fromArray([0, 1]); foreach ($spl as $el1) { foreach ($spl as $el2) { echo "$el1.$el2\n"; } } ?> outputs 0.0 0.1 while you want to get 0.0 0.1 1.0 1.1 It seems to me that this behavior is correct for Iterators; maybe not for IteratorAggregates. Previous Comments: ------------------------------------------------------------------------ [2020-03-23 01:51:35] vlad dot turchinskiy at yandex dot ru cmb@php.net, as far as SPLFixedArray implements Traversable, it is expected to support foreach. I updated code, to make the difference more obvious: https://3v4l.org/N9k7P Please, note, that in the test code i provided, unless we use $collection->getSum() inside the loop - it works as expected. ------------------------------------------------------------------------ [2020-03-22 23:32:58] cmb@php.net Presuming for Iterators foreach ($it as $el) { var_dump($el); } is equivalent to for ($it->rewind(); $it->valid(); $it->next()) { $el = $it->current(); var_dump($el); } the behavior is correct. On the other hand, SplDoublyLinkedList, for instance, supports nested traversal via foreach ... ------------------------------------------------------------------------ [2020-03-22 15:50:30] vlad dot turchinskiy at yandex dot ru Description: ------------ Referencing to SplFixedArray interrupts foreach loop. Test script: --------------- https://3v4l.org/FdN4i Expected result: ---------------- 3 iterations executed. Actual result: -------------- 1 iteration executed. ------------------------------------------------------------------------ [2020-03-22 15:40:32] vlad dot turchinskiy at yandex dot ru Description: ------------ Referencing to SplFixedArray interrupts foreach loop. Test script: --------------- https://3v4l.org/FdN4i Expected result: ---------------- 3 iterations executed. Actual result: -------------- 1 iteration executed. ------------------------------------------------------------------------ [2020-03-22 15:37:45] vlad dot turchinskiy at yandex dot ru Description: ------------ Referencing to SplFixedArray interrupts foreach loop. Test script: --------------- https://3v4l.org/FdN4i Expected result: ---------------- 3 iterations executed. Actual result: -------------- 1 iteration executed. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=79404 -- Edit this bug report at https://bugs.php.net/bug.php?id=79404&edit=1

« previous php.bugs (#226231) next »