1

I am trying to calculate parent names of a person recursively to a specific person ID. I've written recursive call for this but it is behaving weirdly. I've tried to debug it but still don't get what I am doing wrong with it.

Here is my script:

public function full_name_to_root()
{
    $name = $this->name;
    // printf('caller');
    $full_name = FamilyTree::getNameToRoot($this,$name,1);
    dd($full_name);
    return $full_name;
}

public static function getNameToRoot($daleel,$name,$c)
{
    $halt = false;
    $c+=1;
    echo 'in rec. <br>';
    $parent = $daleel->parent;
    if(!$parent){
        $halt = true;
        return $c;
    }elseif ($parent && $parent->family_tree_id==6) {
        echo 'Reached to where we want to stop <br>';
        $name .= ' '.$parent->name;
        $halt = true;
        return $c; // As you can see in output when control reaches it is not stopping the execution, but it is always going to end
    }elseif($parent){
        echo 'call again <br>';
        $name .= ' '.$parent->name;
        
        $halt = false;
        FamilyTree::getNameToRoot($parent,$name,$c);
    }
    // Control should never reach here but it is coming here every time and more annoying thing is that it is printing highest count(or in case of name the most large, which also the required one) at first time and then gradually prints lower
    echo "<br>";
    echo $c;
    echo "<br>";
    // function executes above echo statements 6-7 times but only executes the return statement last time.
    return $c; // I am sending count in response for just testing purpose. It is behaving same as $name is behaving.
} 

I have adding some flags for debugging purpose here is the output of this function. enter image description here

Any help sorting out this issue would be amazing. Thanks!

Community
  • 1
  • 1
tmw
  • 1,424
  • 1
  • 15
  • 26

2 Answers2

0

Yes, it will always continue because you are not stopping it. You have to manually stop with something like this :

....
$halt = false;
while ( !$halt ) 
{
    echo 'in rec. <br>';
    $parent = $daleel->parent;
    ...

    elseif($parent){
        echo 'call again <br>';
        $name .= ' '.$parent->name;

        $halt = false;
        FamilyTree::getNameToRoot($parent,$name,$c);
    }
}

You may have to rearrange a few things inside the while loop but at least it will stop running as soon as $halt is true.

Try this, if it does not work, I may have missed something. Good luck mate!

EddyTheDove
  • 12,979
  • 2
  • 37
  • 45
0

To give you are starting point I would say, that your main problem is caused when you enter this if branch.

    elseif($parent){
        echo 'call again <br>';
        $name .= ' '.$parent->name;

        $halt = false;
        FamilyTree::getNameToRoot($parent,$name,$c);
    }

You don't have a return statement at FamilyTree::getNameToRoot(...)

So the the method does not return and continues to run after the subsequent calls to FamilyTree::getNameToRoot() are finished.

So just use

return FamilyTree::getNameToRoot($parent,$name,$c);

The $halt variable is not needed at all.

shock_gone_wild
  • 6,700
  • 4
  • 28
  • 52