Page 1 of 1

Function recursivity versus variable scope

Posted: Tue Aug 19, 2008 7:49 am
by lordofgore
Hy guys,

I need some advice on <post subject>

I am using recursivity to browse through some classes tree containing references. Mainly think of an object of class tree holding an array of references to another objects:

<?php
class tree {
var $name;

function __construct($leafname){
$this->name = $leafname;
}

function add_leaf ($leafname) {
$this->leafs[count($this->leafs)] = new tree($leafname);
}
}

What I want now is to search recursively through this tree for a particular name let's say and return the object:

function search_tree($obj, $search_string, $rec = false) {
global $ret;
if (!$rec) unset ($ret);

if ($obj->name == $search_string)
$ret = $obj;

if (isset($this->leafs))
foreach ($this->leafs as $leaf)
search_tree ($obj, $search_string, true);

if (!$rec) return $ret;
}


$root = populate_tree_from_db();
search_tree ($root, 'What string you want');
?>

Pretty straightforward I think, ok? The only problem is that the global variable $ret doesn't make it to the first recursion and the function doesn't return anything. I am sure I am missing something but don't know exactly what.
Can anybody help me?

Thank you in advance

Re: Function recursivity versus variable scope

Posted: Tue Aug 19, 2008 9:56 am
by ghurtado
You are not doing anything with the return value of search_tree, therefore, that value is lost.

Re: Function recursivity versus variable scope

Posted: Tue Aug 19, 2008 11:12 am
by lordofgore
Thanks for your reply.

I think I am missing something on variable scope and function recursivity.
I have rebuilt the functions like this:

Code: Select all

 
    function parse_tree($obj, $tpl_phase_id){
            
        global $ret_srch;
        global $lvl;
        
        $lvl++;
        print 'Lvl '.$lvl.': tpl_phase_id '.$tpl_phase_id.' vs. obj '.$obj->phase_info->name.'('.$obj->phase_info->tpl_phase_id.')<BR>';
        print $ret_srch->phase_info->name.'<BR>';
        if ($obj->phase_info->tpl_phase_id == $tpl_phase_id)
            $ret_srch = $obj;
        if (isset($obj->leafs))
            foreach ($obj->leafs as $leaf)
                parse_tree($leaf, $tpl_phase_id);
                
        print 'Lvl: '.$lvl.' ret srch: '.$ret_srch->phase_info->name.'<BR>';
    }
 
    function search_tree ($obj, $tpl_phase_id) {
        
        unset($ret_srch);
        unset ($lvl);
        
        print 'Searching for phase id '.$tpl_phase_id.'<BR>';
        
        parse_tree($obj, $tpl_phase_id);
        
        return $ret_srch;
    }
    
      $leaf1 = search_tree($root, $first_id)
 
       print 'Obj? '.$leaf->phase_info->name;
       
        $leaf2 = search_tree ($root, $second_id);
 
       print 'Obj? '.$leaf->phase_info->name;
 
Output is:

Searching for phase id 16
Lvl 1: tpl_phase_id 16 vs. obj Project root()

Lvl 2: tpl_phase_id 16 vs. obj Aviz Urbanism(16)

Lvl: 2 ret srch: Aviz Urbanism
Lvl 3: tpl_phase_id 16 vs. obj Inca o etapa(20)
Aviz Urbanism
Lvl: 3 ret srch: Aviz Urbanism
Lvl: 3 ret srch: Aviz Urbanism
obj:
Dependinta: 19 - 16
searching for id ... 19
Searching for phase id 19
Lvl 4: tpl_phase_id 19 vs. obj Project root()
Aviz Urbanism
Lvl 5: tpl_phase_id 19 vs. obj Aviz Urbanism(16)
Aviz Urbanism
Lvl: 5 ret srch: Aviz Urbanism
Lvl 6: tpl_phase_id 19 vs. obj Inca o etapa(20)
Aviz Urbanism
Lvl: 6 ret srch: Aviz Urbanism
Lvl: 6 ret srch: Aviz Urbanism
Obj?


So not only the value doesn't return to the main code but inside the function parse_tree() the values of $lvl and $ret_srch are not destroyed when the function is ended and not even when search_tree is called again and both variables are unset.

What is the reason for this strange behaviour?

Thank you

Re: Function recursivity versus variable scope

Posted: Tue Aug 19, 2008 11:51 am
by ghurtado
Please add "code" tags to your post, it makes the source code much easier to read.

Re: Function recursivity versus variable scope

Posted: Tue Aug 19, 2008 11:54 am
by prowebpro
you must becareful tough with recursivity

Re: Function recursivity versus variable scope

Posted: Tue Aug 19, 2008 11:57 am
by lordofgore
ghurtado wrote:Please add "code" tags to your post, it makes the source code much easier to read.
done

Re: Function recursivity versus variable scope

Posted: Tue Aug 19, 2008 12:12 pm
by ghurtado
I think part of the problem might be that you declared the variables as "global" in one of the functions, but not the other. Declare them as global in search_tree as well so that the function will be referring to the same variables as parse_tree.