My goal is to display a family tree starting from Person X and showing all descendants. No need to show siblings, parents, or other ancestors.
To this end I have a person
class.
I also have a database table with person_ID
and parent_ID
columns.
When the person
class is created you pass it the desired person ID, then it will preload its parents and children IDs from that table.
In order to create the descendents tree I wrote the following method inside the person
class:
public function loadChildren() {
foreach ($this->_ChildIDs as $curChildID) {
$child = new Person();
$child->loadSelfFromID($curChildID);
$child->loadChildren();
$this->_Children[] = $child;
}
}
This successfully recursively loads the entire tree of descendents.
So far so good.
In order to display this data as a nested HTML list I wrote the following standalone function:
function recursivePrint($array) {
echo '<ul>';
foreach ($array as $child) {
echo '<li>'.$child->getName();
recursivePrint($child->getChildren());
echo '</li>';
}
echo '</ul>';
}
The final script then looks like this:
$c = new Person();
$c->loadSelfFromID('1');
$c->loadChildren(); // recursively loads all descendants
$descendants = $c->getChildren();
recursivePrint($descendants);
//Output is as expected.
My question is this: where do I stick that standalone function?
Does it just get tossed in a random utility include for functions that don't really go anywhere else? Should it go into the person
class? Should it go into a FamilyTree
class that only makes trees?