3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CategoryTree { public $tree; private $failure = false; public function __construct() { // 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 $this->failure; // 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 $branch; } 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 $this->failure; } public function &getCategoryWithName($name) { $tree = &$this->tree; return $this->getCategoryWithNameInSubtree($name, $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/t167c
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                                                   !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 __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/t167c
function name:  __construct
number of ops:  5
compiled vars:  !0 = $tree
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   FETCH_OBJ_R                                      ~1      'tree'
          1        ASSIGN                                                   !0, ~1
   11     2        ASSIGN_DIM                                               !0, 'root'
          3        OP_DATA                                                  <array>
   12     4      > RETURN                                                   null

End of function __construct

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

End of function getcategorywithnameinsubtree

Function getcategorywithname:
Finding entry points
Branch analysis from position: 0
Return found
filename:       /in/t167c
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_VAR                                                 !0
          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.0.0


preferences:
169.19 ms | 1400 KiB | 15 Q