php-src/ext/dom/tests/unpack_foreach_behaviour.phpt
Niels Dossche 9be9f70caa
Fix weird unpack behaviour in DOM
Engine pitfall: the iter index is only updated by foreach opcodes, so
the existing code that used it as an index for the nodes w.r.t. the
start did not work properly. Fix it by using our own counter.

Closes GH-18004.
2025-03-09 11:17:03 +01:00

31 lines
546 B
PHP

--TEST--
Unpacking vs foreach behaviour
--EXTENSIONS--
dom
--FILE--
<?php
$dom = new DOMDocument;
$dom->loadXML('<root><a/><b/></root>');
echo "--- By foreach: ---\n";
foreach ($dom->documentElement->getElementsByTagName('*') as $node) {
var_dump($node->localName);
}
echo "--- By unpacking: ---\n";
$iter = $dom->documentElement->getElementsByTagName('*');
foreach ([...$iter] as $node) {
var_dump($node->localName);
}
?>
--EXPECT--
--- By foreach: ---
string(1) "a"
string(1) "b"
--- By unpacking: ---
string(1) "a"
string(1) "b"