What are some common challenges faced when creating a family tree structure in PHP?

One common challenge when creating a family tree structure in PHP is handling recursive relationships between family members, such as parents and children. To solve this, you can use a recursive function to traverse the family tree and build the structure accordingly.

class Person {
    public $name;
    public $children = [];

    public function addChild(Person $child) {
        $this->children[] = $child;
    }
}

function buildFamilyTree($person) {
    echo $person->name . "\n";
    foreach ($person->children as $child) {
        buildFamilyTree($child);
    }
}

// Example usage
$grandparent = new Person();
$parent = new Person();
$child1 = new Person();
$child2 = new Person();

$grandparent->addChild($parent);
$parent->addChild($child1);
$parent->addChild($child2);

buildFamilyTree($grandparent);