PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » HTML » HTML_TreeMenu » Bug #1015

addItem divergent behavior

Details

Submitted2004-03-15 09:24 UTC
Fromwbwither at yahoo dot com
StatusBogus
PackageHTML_TreeMenu
PHP Version4.3.0
OSOS X
Roadmaps(Not assigned)

Comments

[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).

[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.