Class member variable bug???
| From: | David A. Waltermire | Date: | Fri, 21 Jan 2000 18:49:15 +0000 |
| Subject: | Class member variable bug??? | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-9341@lists.php.net to get a copy of this message | ||
I think I found a bug in the class code. Here is my class:
class MenuNode
{
var $branches=array(); // Array of attached class(MenuNode)
var $branch_rows=0; // Rows in branches array
var $url=''; // Link's url
var $description=''; // Link's text
function MenuNode($desc,$url)
{
// Set resonable defaults
$this->description = $desc;
$this->url = $url;
}
function addBranch($Node)
{
if (!isset($Node))
{
print "addBranch: NULL branch!\n";
return false;
}
$this->branches[$this->branch_rows]=$Node;
print "Attached new sub-branch to <b>" . $this->description .
"->branches[" . $this->branch_rows . "]</b><br>\n";
print "New branch <b>description</b> = <b>" . $Node->description
. "</b><br>\n";
print "New branch <b>url</b> = <b>" . $Node->url .
"</b><br>\n";
$this->branch_rows++;
echo "<b>" . $this->description . "->branch_rows</b> incremented
to <b>"
. $this->branch_rows . "</b><br>\n";
return true;
}
function show($level=0)
{
print "<br>\n";
$i=0;
while ($i<$level)
{
print " ";
$i++;
}
print "url = " . $this->url . "<br>\n";
$i=0;
while ($i<$level)
{
print " ";
$i++;
}
print "description = " . $this->description . "<br>\n";
$i=0;
while ($i<$level)
{
print " ";
$i++;
}
print "branch count = " . $this->branch_rows . "<br>\n";
for ($i=0;$i<$this->branch_rows;$i++)
{
if (!isset($this->branches[$i]))
print "branch not set!<p>";
$this->branches[$i]->show($level+1);
}
}
}
Here is my test code:
$mn_1=new MenuNode("Main1","http://main1.com");
echo "<hr>";
$mn_2=new MenuNode("Sub2","http://sub2.main.com");
$mn_1->addBranch($mn_2);
echo "<hr>";
$mn_3=new MenuNode("Sub3","http://sub3.main.com");
$mn_1->addBranch($mn_3);
echo "<hr>";
$mn_4=new MenuNode("Sub4","http://sub4.main.com");
$mn_2->addBranch($mn_4);
echo "<hr>";
$mn_5=new MenuNode("Sub5","http://sub5.main.com");
$mn_3->addBranch($mn_5);
echo "<hr>";
$mn_6=new MenuNode("Sub6","http://sub6.main.com");
$mn_2->addBranch($mn_6);
echo "<hr>";
$mn_7=new MenuNode("Sub7","http://sub7.main.com");
$mn_3->addBranch($mn_7);
echo "<hr>";
$mn_1->show();
Here is the output:
Attached new sub-branch to Main1->branches[0]
New branch description = Sub2
New branch url = http://sub2.main.com
Main1->branch_rows incremented to 1
Attached new sub-branch to Main1->branches[1]
New branch description = Sub3
New branch url = http://sub3.main.com
Main1->branch_rows incremented to 2
Attached new sub-branch to Sub2->branches[0]
New branch description = Sub4
New branch url = http://sub4.main.com
Sub2->branch_rows incremented to 1
Attached new sub-branch to Sub3->branches[0]
New branch description = Sub5
New branch url = http://sub5.main.com
Sub3->branch_rows incremented to 1
Attached new sub-branch to Sub2->branches[1]
New branch description = Sub6
New branch url = http://sub6.main.com
Sub2->branch_rows incremented to 2
Attached new sub-branch to Sub3->branches[1]
New branch description = Sub7
New branch url = http://sub7.main.com
Sub3->branch_rows incremented to 2
url = http://main1.com
description = Main1
branch count = 2
url = http://sub2.main.com
description = Sub2
branch count = 0
url = http://sub3.main.com
description = Sub3
branch count = 0
Here is how the nodes are attached using my test code:
Node1 has Node2 and Node3 attached
Node2 has Node4 and Node6 attached
Node3 has Node5 and Node7 attached
The bottom potion of the test code displays the nesting, each node's
content and how many attached nodes. I call show on Node1 and it then
recurses through the attached nodes. Unfortuatly, the branch counts on
Nodes 2 and 3 are displayed as being 0 not 2. It appears that PHP4 is
"losing" their values, but not the other member variables values since
they are displayed corectly. This appears to be a bug in the engion.
Anyone see anything I am doing wrong?
--
David A. Waltermire (mailto:dwalt@supernet.com)
Systems Administration Manager
Phone: 717-738-7071 Fax: 717-738-7030
D&E SuperNet, A OneMain.com Company
http://www.desupernet.net or http://www.onemain.com
Attachment: [text/x-vcard] Card for David A. Waltermire dwalt.vcf
Attachment: [text/x-vcard] Card for David A. Waltermire dwalt.vcf