3v4l.org

run code in 500+ PHP versions simultaneously
<?php class CategoryTree { var $tree; public function CategoryTree() { // First element of the array represents the name of the category, so to iterate children, we have to go from index 1 $tree=$this->tree; $tree["root"] = array(); } private function &getCategoryWithNameInSubtree($name, &$subTreeRoot) { if (count($subTreeRoot) == 0) return false; // There are no branches coming from this root // So, the subtree has some branches to traverse... foreach ($subTreeRoot as $branchName => &$branch) { if ($branchName == $name) { // Search is over - this branch has the specified name return $subTreeRoot[$branchName]; } else { $subTreeSearchResult = $this->getCategoryWithNameInSubtree($name, $branch); if($subTreeSearchResult) { return $subTreeSearchResult; } else { //If we have reached this, it means the name was not found in that branch } } } //We traversed all branches and no name was equal to the specified name return false; } public function &getCategoryWithName($name) { $tree=&$this->tree; return $this->getCategoryWithNameInSubtree("seconda", $tree); } } $c = new CategoryTree(); $c->tree=array("prima" => array("prima-prima" => [], "prima-seconda" => [], "prima-terza" => []), "seconda" => array("seconda-prima" => [], "seconda-seconda" => [], "seconda-terza" => []), "terza" => array("terza-prima" => [], "terza-seconda" => [], "terza-terza" => []), ); $seconda=&$c->getCategoryWithName("seconda"); $seconda[] = "added"; print "cat is: <pre>"; print_r($seconda); print "</pre>"; print "ct is: <pre>"; print_r($c); print "</pre>";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9fF0c
function name:  (null)
number of ops:  22
compiled vars:  !0 = $c, !1 = $seconda
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   45     0  E >   NEW                                                  $2      'CategoryTree'
          1        DO_FCALL                                          0          
          2        ASSIGN                                                       !0, $2
   47     3        ASSIGN_OBJ                                                   !0, 'tree'
          4        OP_DATA                                                      <array>
   53     5        INIT_METHOD_CALL                                             !0, 'getCategoryWithName'
          6        SEND_VAL_EX                                                  'seconda'
          7        DO_FCALL                                          0  $6      
          8        ASSIGN_REF                                                   !1, $6
   54     9        ASSIGN_DIM                                                   !1
         10        OP_DATA                                                      'added'
   55    11        ECHO                                                         'cat+is%3A+%3Cpre%3E'
         12        INIT_FCALL                                                   'print_r'
         13        SEND_VAR                                                     !1
         14        DO_ICALL                                                     
         15        ECHO                                                         '%3C%2Fpre%3E'
   57    16        ECHO                                                         'ct+is%3A+%3Cpre%3E'
         17        INIT_FCALL                                                   'print_r'
         18        SEND_VAR                                                     !0
         19        DO_ICALL                                                     
         20        ECHO                                                         '%3C%2Fpre%3E'
         21      > RETURN                                                       1

Class CategoryTree:
Function categorytree:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9fF0c
function name:  CategoryTree
number of ops:  5
compiled vars:  !0 = $tree
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    9     0  E >   FETCH_OBJ_R                                          ~1      'tree'
          1        ASSIGN                                                       !0, ~1
   10     2        ASSIGN_DIM                                                   !0, 'root'
          3        OP_DATA                                                      <array>
   11     4      > RETURN                                                       null

End of function categorytree

Function getcategorywithnameinsubtree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
Return found
Branch analysis from position: 6
2 jumps found. (Code = 125) Position 1 = 7, Position 2 = 25
Branch analysis from position: 7
2 jumps found. (Code = 126) Position 1 = 8, Position 2 = 25
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
Return found
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 24
Branch analysis from position: 21
Return found
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 25
Return found
Branch analysis from position: 25
filename:       /in/9fF0c
function name:  getCategoryWithNameInSubtree
number of ops:  28
compiled vars:  !0 = $name, !1 = $subTreeRoot, !2 = $branch, !3 = $branchName, !4 = $subTreeSearchResult
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   13     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   15     2        COUNT                                                ~5      !1
          3        IS_EQUAL                                                     ~5, 0
          4      > JMPZ                                                         ~6, ->6
          5    > > RETURN_BY_REF                                                <false>
   18     6    > > FE_RESET_RW                                          $7      !1, ->25
          7    > > FE_FETCH_RW                                          ~8      $7, !2, ->25
          8    >   ASSIGN                                                       !3, ~8
   19     9        IS_EQUAL                                                     !3, !0
         10      > JMPZ                                                         ~10, ->15
   20    11    >   FETCH_DIM_W                                          $11     !1, !3
         12        FE_FREE                                                      $7
         13      > RETURN_BY_REF                                                $11
   19    14*       JMP                                                          ->24
   22    15    >   INIT_METHOD_CALL                                             'getCategoryWithNameInSubtree'
         16        SEND_VAR                                                     !0
         17        SEND_REF                                                     !2
         18        DO_FCALL                                          0  $12     
         19        ASSIGN                                                       !4, $12
   23    20      > JMPZ                                                         !4, ->24
   24    21    >   FE_FREE                                                      $7
         22      > RETURN_BY_REF                                                !4
   23    23*       JMP                                                          ->24
   18    24    > > JMP                                                          ->7
         25    >   FE_FREE                                                      $7
   33    26      > RETURN_BY_REF                                                <false>
   35    27*     > RETURN_BY_REF                                                null

End of function getcategorywithnameinsubtree

Function getcategorywithname:
Finding entry points
Branch analysis from position: 0
Return found
filename:       /in/9fF0c
function name:  getCategoryWithName
number of ops:  9
compiled vars:  !0 = $name, !1 = $tree
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   37     0  E >   RECV                                                 !0      
   38     1        FETCH_OBJ_W                                          $2      'tree'
          2        ASSIGN_REF                                                   !1, $2
   39     3        INIT_METHOD_CALL                                             'getCategoryWithNameInSubtree'
          4        SEND_VAL                                                     'seconda'
          5        SEND_REF                                                     !1
          6        DO_FCALL                                          0  $4      
          7      > RETURN_BY_REF                                                $4
   41     8*     > RETURN_BY_REF                                                null

End of function getcategorywithname

End of class CategoryTree.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
235.14 ms | 2516 KiB | 14 Q