By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,422 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,422 IT Pros & Developers. It's quick & easy.

Recursive "single-referenced" methods and static variables

P: n/a
[PHP]
class DepartmentTree {

function &buildTree($id) {
static $html;
if (!isset($html)) {
// DO STUFF HERE TO $html
}
// DO MORE STUFF
if ($condition_is_met) $this->buildTree($newID);
$this->html = $html;
}

}
[/PHP]

The above (and greatly paraphrased from the 100-line actual class)
class and method, in its real form, totally works inasmuch as it
successfully always creates an HTML hierarchial tree of departments
mapped with child departments. I have no problem with this.. as long
as I only use the class once.

However, there is a case where I need the entire contents of the
departments table dumped out into a resultset, and to do that I
figured I would just loop through a query of records that have no
parent ID; each id in each row I seed into $tree->buildTree():

[PHP]
for ($i = 0; $i < @sizeof($result); $i++) {
$tree->buildTree($result[$i]->id);
$this->deptArray += $tree->convert_to_array();
$tree->clearTreeHTML();
}
[/PHP]

The "convert_to_array()" method will convert the contents of
$this->html from HTML content to an array, keeping the original
hierarchial order; the "clearTreeHTML()" method will set $this->html
to NULL or "".

Problem is, it does not do that, because apparently "static $html"
keeps an instance of it running in the single-referenced instance of
"buildTree" method.

Based on how best I can explain my problem, and sorry I can't explain
it any better w/o dumping the actual code line by line, how have you
all figured out the best way to generate multiple, unique instances
from a single-referenced method that uses a static local variable?

Thanx
Phil
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.