3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CategoryTree { private $catTree = array(); public function addCategory($category, $parent) { if($parent == null){ if(count(array_column($this->catTree, 'category', $category)) > 0){ echo 'Duplicate category'; } else{ $temp = array("category" => $category,"parent" => $parent); array_push($this->catTree,$temp); } } else{ echo 'Parent: ' . $parent; print_r(count(array_column($this->catTree, 'category'), $parent)); } } public function getChildren($parent) { $that = $this; $keys = array_keys(array_column($this->catTree, 'parent'), $parent); return array_map(function($k) use ($that){return $that->catTree[$k]['category'];}, $keys); } } // For testing purposes (do not submit uncommented): $c = new CategoryTree; $c->addCategory('A', null); $c->addCategory('A', null); $c->addCategory('B', 'A'); $c->addCategory('B', 'D'); $c->addCategory('C', 'A'); echo implode(',', $c->getChildren('A'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/gkUgf
function name:  (null)
number of ops:  32
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   NEW                                              $1      'CategoryTree'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   35     3        INIT_METHOD_CALL                                         !0, 'addCategory'
          4        SEND_VAL_EX                                              'A'
          5        SEND_VAL_EX                                              null
          6        DO_FCALL                                      0          
   36     7        INIT_METHOD_CALL                                         !0, 'addCategory'
          8        SEND_VAL_EX                                              'A'
          9        SEND_VAL_EX                                              null
         10        DO_FCALL                                      0          
   37    11        INIT_METHOD_CALL                                         !0, 'addCategory'
         12        SEND_VAL_EX                                              'B'
         13        SEND_VAL_EX                                              'A'
         14        DO_FCALL                                      0          
   38    15        INIT_METHOD_CALL                                         !0, 'addCategory'
         16        SEND_VAL_EX                                              'B'
         17        SEND_VAL_EX                                              'D'
         18        DO_FCALL                                      0          
   39    19        INIT_METHOD_CALL                                         !0, 'addCategory'
         20        SEND_VAL_EX                                              'C'
         21        SEND_VAL_EX                                              'A'
         22        DO_FCALL                                      0          
   41    23        INIT_FCALL                                               'implode'
         24        SEND_VAL                                                 '%2C'
         25        INIT_METHOD_CALL                                         !0, 'getChildren'
         26        SEND_VAL_EX                                              'A'
         27        DO_FCALL                                      0  $9      
         28        SEND_VAR                                                 $9
         29        DO_ICALL                                         $10     
         30        ECHO                                                     $10
         31      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FgkUgf%3A28%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/gkUgf
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $k, !1 = $that
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        FETCH_OBJ_R                                      ~2      !1, 'catTree'
          3        FETCH_DIM_R                                      ~3      ~2, !0
          4        FETCH_DIM_R                                      ~4      ~3, 'category'
          5      > RETURN                                                   ~4
          6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FgkUgf%3A28%240

Class CategoryTree:
Function addcategory:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 24
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/gkUgf
function name:  addCategory
number of ops:  39
compiled vars:  !0 = $category, !1 = $parent, !2 = $temp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    8     2        IS_EQUAL                                                 !1, null
          3      > JMPZ                                                     ~3, ->24
    9     4    >   INIT_FCALL                                               'array_column'
          5        FETCH_OBJ_R                                      ~4      'catTree'
          6        SEND_VAL                                                 ~4
          7        SEND_VAL                                                 'category'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $5      
         10        COUNT                                            ~6      $5
         11        IS_SMALLER                                               0, ~6
         12      > JMPZ                                                     ~7, ->15
   10    13    >   ECHO                                                     'Duplicate+category'
         14      > JMP                                                      ->23
   13    15    >   INIT_ARRAY                                       ~8      !0, 'category'
         16        ADD_ARRAY_ELEMENT                                ~8      !1, 'parent'
         17        ASSIGN                                                   !2, ~8
   14    18        INIT_FCALL                                               'array_push'
         19        FETCH_OBJ_W                                      $10     'catTree'
         20        SEND_REF                                                 $10
         21        SEND_VAR                                                 !2
         22        DO_ICALL                                                 
         23    > > JMP                                                      ->38
   18    24    >   CONCAT                                           ~12     'Parent%3A+', !1
         25        ECHO                                                     ~12
   19    26        INIT_FCALL                                               'print_r'
         27        INIT_FCALL                                               'count'
         28        INIT_FCALL                                               'array_column'
         29        FETCH_OBJ_R                                      ~13     'catTree'
         30        SEND_VAL                                                 ~13
         31        SEND_VAL                                                 'category'
         32        DO_ICALL                                         $14     
         33        SEND_VAR                                                 $14
         34        SEND_VAR                                                 !1
         35        DO_ICALL                                         $15     
         36        SEND_VAR                                                 $15
         37        DO_ICALL                                                 
   22    38    > > RETURN                                                   null

End of function addcategory

Function getchildren:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/gkUgf
function name:  getChildren
number of ops:  21
compiled vars:  !0 = $parent, !1 = $that, !2 = $keys
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   26     1        FETCH_THIS                                       ~3      
          2        ASSIGN                                                   !1, ~3
   27     3        INIT_FCALL                                               'array_keys'
          4        INIT_FCALL                                               'array_column'
          5        FETCH_OBJ_R                                      ~5      'catTree'
          6        SEND_VAL                                                 ~5
          7        SEND_VAL                                                 'parent'
          8        DO_ICALL                                         $6      
          9        SEND_VAR                                                 $6
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                         $7      
         12        ASSIGN                                                   !2, $7
   28    13        INIT_FCALL                                               'array_map'
         14        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FgkUgf%3A28%240'
         15        BIND_LEXICAL                                             ~9, !1
         16        SEND_VAL                                                 ~9
         17        SEND_VAR                                                 !2
         18        DO_ICALL                                         $10     
         19      > RETURN                                                   $10
   29    20*     > RETURN                                                   null

End of function getchildren

End of class CategoryTree.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.67 ms | 1396 KiB | 27 Q