1--TEST-- 2DOMDocument::getElementsByTagName() liveness tree walk 3--EXTENSIONS-- 4dom 5--FILE-- 6<?php 7 8$doc = new DOMDocument; 9$doc->loadXML('<root><container><a><b i="1"/><b i="2"/></a><b i="3"/></container><b i="4"/></root>'); 10 11echo "-- On first child, for --\n"; 12$list = $doc->documentElement->firstChild->getElementsByTagName('b'); 13var_dump($list->length); 14for ($i = 0; $i < $list->length; $i++) { 15 echo $i, " ", $list->item($i)->getAttribute('i'), "\n"; 16} 17// Try to access one beyond to check if we don't get excess elements 18var_dump($list->item($i)); 19 20echo "-- On first child, foreach --\n"; 21foreach ($list as $item) { 22 echo $item->getAttribute('i'), "\n"; 23} 24 25echo "-- On document, for --\n"; 26$list = $doc->getElementsByTagName('b'); 27var_dump($list->length); 28for ($i = 0; $i < $list->length; $i++) { 29 echo $i, " ", $list->item($i)->getAttribute('i'), "\n"; 30} 31// Try to access one beyond to check if we don't get excess elements 32var_dump($list->item($i)); 33 34echo "-- On document, foreach --\n"; 35foreach ($list as $item) { 36 echo $item->getAttribute('i'), "\n"; 37} 38 39echo "-- On document, after caching followed by removing --\n"; 40 41$list = $doc->documentElement->firstChild->getElementsByTagName('b'); 42$list->item(0); // Activate item cache 43$list->item(0)->remove(); 44$list->item(0)->remove(); 45$list->item(0)->remove(); 46var_dump($list->length); 47var_dump($list->item(0)); 48foreach ($list as $item) { 49 echo "Should not execute\n"; 50} 51 52echo "-- On document, clean list after removal --\n"; 53$list = $doc->documentElement->firstChild->getElementsByTagName('b'); 54var_dump($list->length); 55var_dump($list->item(0)); 56foreach ($list as $item) { 57 echo "Should not execute\n"; 58} 59 60?> 61--EXPECT-- 62-- On first child, for -- 63int(3) 640 1 651 2 662 3 67NULL 68-- On first child, foreach -- 691 702 713 72-- On document, for -- 73int(4) 740 1 751 2 762 3 773 4 78NULL 79-- On document, foreach -- 801 812 823 834 84-- On document, after caching followed by removing -- 85int(0) 86NULL 87-- On document, clean list after removal -- 88int(0) 89NULL 90