Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.2.2

Bug #1015 addItem divergent behavior
Submitted: 2004-03-15 09:24 UTC
From: wbwither at yahoo dot com Assigned:
Status: Bogus Package: HTML_TreeMenu
PHP Version: 4.3.0 OS: OS X
Roadmaps: (Not assigned)    
Subscription  
Comments Add Comment Add patch


Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem : 48 - 25 = ?

 
 [2004-03-15 09:24 UTC] wbwither at yahoo dot com
Description: ------------ Basically, addItem($node) and addItem(new HTML_TreeNode(...)) produce differing results when the same node is added to the tree. In particular, when the text value is read in from an array in a foreach loop, the addItem($node) (where $node was defined in the line before) will always use the *last* value in the loop, while addItem(new HTML_TreeNode(...)) will use the correct, iterated values from the array. I got this behavior with PHP 4.3.0 on OS X and 4.2.3 on Linux. Reproduce code: --------------- http://www.wbwither.net/TreeMenu/docs/example2.php the crux: foreach($array as $key => $value) { $node00= new HTML_TreeNode(); $node00= new HTML_TreeNode(array('text' => "$value", 'link' => "test.php", 'icon' => $icon, 'expandedIcon' => $expandedIcon, 'expanded' => true), array('onclick' => "alert('foo'); return false", 'onexpand' => "alert('Expanded')")); $menu00->addItem($node00); $menu00->addItem(new HTML_TreeNode(array('text' => "$value", 'link' => "test.php", 'icon' => $icon, 'expandedIcon' => $expandedIcon, 'expanded' => true), array('onclick' => "alert('foo'); return false", 'onexpand' => "alert('Expanded')"))); } Expected result: ---------------- The two methods (run one after the other) should produce the same result, i.e. part1 part1 part2 part2 part3 part3 Actual result: -------------- The method of setting $node and then using addItem($node) will always "stick" on the *last* array value, i.e. part3 part1 part3 part2 part3 part3 I am guessing that this has something to do with the node's text value (and possibly other values?) not being dereferenced properly. It just seems like a pointer error to me, although I can't see anyplace in the class where it might arise. (granted I'm not so good with PHP classes) A workaround is to create an increment variable $i and have the node be called $$i so each new node is called something different. Then it works fine (as long as you're not recursing and re-using the $i values.... although randomness helps).

Comments

 [2004-03-15 19:08 UTC] richard at php dot net
Use a reference (the &) when doing it this way: $node00 = &new HTML_TreeNode(...
 [2004-03-15 19:47 UTC] wbwither at yahoo dot com
my apologies! Is there any way to delete this bug? I feel like a jackass.