lordofgore Posted August 19, 2008 Share Posted August 19, 2008 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 Link to comment https://forums.phpfreaks.com/topic/120350-function-recursivity-versus-variable-scope/ Share on other sites More sharing options...
lordofgore Posted August 19, 2008 Author Share Posted August 19, 2008 I think I am missing something on variable scope and function recursivity. I have rebuilt the functions like this: 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 Link to comment https://forums.phpfreaks.com/topic/120350-function-recursivity-versus-variable-scope/#findComment-620191 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.