3v4l.org

run code in 500+ PHP versions simultaneously
<?php error_reporting(-1); header('Content-Type: text/plain; charset="utf-8"'); class NodeIterator extends \RecursiveArrayIterator { /** * @inheritdoc */ public function hasChildren() { return $this->current()->hasChildren(); } /** * @inheritdoc */ public function getChildren() { return new NodeIterator($this->current()->getChildren()); } } class Node implements \JsonSerializable, \IteratorAggregate { /** * @var mixed */ private $data; /** * @var Node[] */ private $children = []; /** * Node constructor. * * @param mixed $data */ public function __construct($data) { $this->data = $data; } /** * @return mixed */ public function getData() { return $this->data; } /** * @return bool */ public function hasChildren() { return count($this->children) > 0; } /** * @return \RecursiveArrayIterator */ public function getChildren() { return new \RecursiveArrayIterator($this->children); } /** * @param Node $child * * @return Node */ public function addChild(self $child) { if (in_array($child, $this->children, true)) { throw new \InvalidArgumentException('duplicate'); } $this->children[] = $child; return $child; } /** * @inheritdoc */ public function jsonSerialize() { return [ 'data' => $this->data, 'children' => $this->children ]; } /** * @inheritdoc */ public function getIterator() { return new NodeIterator([$this]); } } $root = new Node('root'); $root->addChild(new Node('sub1'))->addChild(new Node('sub1.1')); $root->addChild(new Node('sub2'))->addChild(new Node('sub2.1'))->addChild(new Node('sub.2.1.1')); $root->addChild(new Node('sub3')); print_r(json_encode($root, JSON_PRETTY_PRINT)); echo "\n\n"; foreach ($it = new RecursiveIteratorIterator(new NodeIterator([$root]), RecursiveIteratorIterator::SELF_FIRST) as $node) { echo str_repeat('.', $it->getDepth()), $node->getData(), "\n"; } echo "\n\n"; foreach ($it = new RecursiveIteratorIterator($root, RecursiveIteratorIterator::SELF_FIRST) as $node) { echo str_repeat('.', $it->getDepth()), $node->getData(), "\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 65, Position 2 = 78
Branch analysis from position: 65
2 jumps found. (Code = 78) Position 1 = 66, Position 2 = 78
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
Branch analysis from position: 78
2 jumps found. (Code = 77) Position 1 = 86, Position 2 = 99
Branch analysis from position: 86
2 jumps found. (Code = 78) Position 1 = 87, Position 2 = 99
Branch analysis from position: 87
1 jumps found. (Code = 42) Position 1 = 86
Branch analysis from position: 86
Branch analysis from position: 99
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 99
Branch analysis from position: 78
filename:       /in/D0GfX
function name:  (null)
number of ops:  101
compiled vars:  !0 = $root, !1 = $it, !2 = $node
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    2     0  E >   INIT_FCALL                                                   'error_reporting'
          1        SEND_VAL                                                     -1
          2        DO_ICALL                                                     
    3     3        INIT_FCALL                                                   'header'
          4        SEND_VAL                                                     'Content-Type%3A+text%2Fplain%3B+charset%3D%22utf-8%22'
          5        DO_ICALL                                                     
   26     6        DECLARE_CLASS                                                'node'
  108     7        NEW                                                  $5      'Node'
          8        SEND_VAL_EX                                                  'root'
          9        DO_FCALL                                          0          
         10        ASSIGN                                                       !0, $5
  109    11        INIT_METHOD_CALL                                             !0, 'addChild'
         12        NEW                                                  $8      'Node'
         13        SEND_VAL_EX                                                  'sub1'
         14        DO_FCALL                                          0          
         15        SEND_VAR_NO_REF_EX                                           $8
         16        DO_FCALL                                          0  $10     
         17        INIT_METHOD_CALL                                             $10, 'addChild'
         18        NEW                                                  $11     'Node'
         19        SEND_VAL_EX                                                  'sub1.1'
         20        DO_FCALL                                          0          
         21        SEND_VAR_NO_REF_EX                                           $11
         22        DO_FCALL                                          0          
  110    23        INIT_METHOD_CALL                                             !0, 'addChild'
         24        NEW                                                  $14     'Node'
         25        SEND_VAL_EX                                                  'sub2'
         26        DO_FCALL                                          0          
         27        SEND_VAR_NO_REF_EX                                           $14
         28        DO_FCALL                                          0  $16     
         29        INIT_METHOD_CALL                                             $16, 'addChild'
         30        NEW                                                  $17     'Node'
         31        SEND_VAL_EX                                                  'sub2.1'
         32        DO_FCALL                                          0          
         33        SEND_VAR_NO_REF_EX                                           $17
         34        DO_FCALL                                          0  $19     
         35        INIT_METHOD_CALL                                             $19, 'addChild'
         36        NEW                                                  $20     'Node'
         37        SEND_VAL_EX                                                  'sub.2.1.1'
         38        DO_FCALL                                          0          
         39        SEND_VAR_NO_REF_EX                                           $20
         40        DO_FCALL                                          0          
  111    41        INIT_METHOD_CALL                                             !0, 'addChild'
         42        NEW                                                  $23     'Node'
         43        SEND_VAL_EX                                                  'sub3'
         44        DO_FCALL                                          0          
         45        SEND_VAR_NO_REF_EX                                           $23
         46        DO_FCALL                                          0          
  113    47        INIT_FCALL                                                   'print_r'
         48        INIT_FCALL                                                   'json_encode'
         49        SEND_VAR                                                     !0
         50        SEND_VAL                                                     128
         51        DO_ICALL                                             $26     
         52        SEND_VAR                                                     $26
         53        DO_ICALL                                                     
  114    54        ECHO                                                         '%0A%0A'
  116    55        NEW                                                  $28     'RecursiveIteratorIterator'
         56        NEW                                                  $29     'NodeIterator'
         57        INIT_ARRAY                                           ~30     !0
         58        SEND_VAL_EX                                                  ~30
         59        DO_FCALL                                          0          
         60        SEND_VAR_NO_REF_EX                                           $29
         61        SEND_VAL_EX                                                  1
         62        DO_FCALL                                          0          
         63        ASSIGN                                               ~33     !1, $28
         64      > FE_RESET_R                                           $34     ~33, ->78
         65    > > FE_FETCH_R                                                   $34, !2, ->78
  117    66    >   INIT_FCALL                                                   'str_repeat'
         67        SEND_VAL                                                     '.'
         68        INIT_METHOD_CALL                                             !1, 'getDepth'
         69        DO_FCALL                                          0  $35     
         70        SEND_VAR                                                     $35
         71        DO_ICALL                                             $36     
         72        ECHO                                                         $36
         73        INIT_METHOD_CALL                                             !2, 'getData'
         74        DO_FCALL                                          0  $37     
         75        ECHO                                                         $37
         76        ECHO                                                         '%0A'
  116    77      > JMP                                                          ->65
         78    >   FE_FREE                                                      $34
  120    79        ECHO                                                         '%0A%0A'
  122    80        NEW                                                  $38     'RecursiveIteratorIterator'
         81        SEND_VAR_EX                                                  !0
         82        SEND_VAL_EX                                                  1
         83        DO_FCALL                                          0          
         84        ASSIGN                                               ~40     !1, $38
         85      > FE_RESET_R                                           $41     ~40, ->99
         86    > > FE_FETCH_R                                                   $41, !2, ->99
  123    87    >   INIT_FCALL                                                   'str_repeat'
         88        SEND_VAL                                                     '.'
         89        INIT_METHOD_CALL                                             !1, 'getDepth'
         90        DO_FCALL                                          0  $42     
         91        SEND_VAR                                                     $42
         92        DO_ICALL                                             $43     
         93        ECHO                                                         $43
         94        INIT_METHOD_CALL                                             !2, 'getData'
         95        DO_FCALL                                          0  $44     
         96        ECHO                                                         $44
         97        ECHO                                                         '%0A'
  122    98      > JMP                                                          ->86
         99    >   FE_FREE                                                      $41
  124   100      > RETURN                                                       1

Class NodeIterator:
Function haschildren:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D0GfX
function name:  hasChildren
number of ops:  6
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   13     0  E >   INIT_METHOD_CALL                                             'current'
          1        DO_FCALL                                          0  $0      
          2        INIT_METHOD_CALL                                             $0, 'hasChildren'
          3        DO_FCALL                                          0  $1      
          4      > RETURN                                                       $1
   14     5*     > RETURN                                                       null

End of function haschildren

Function getchildren:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D0GfX
function name:  getChildren
number of ops:  9
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   21     0  E >   NEW                                                  $0      'NodeIterator'
          1        INIT_METHOD_CALL                                             'current'
          2        DO_FCALL                                          0  $1      
          3        INIT_METHOD_CALL                                             $1, 'getChildren'
          4        DO_FCALL                                          0  $2      
          5        SEND_VAR_NO_REF_EX                                           $2
          6        DO_FCALL                                          0          
          7      > RETURN                                                       $0
   22     8*     > RETURN                                                       null

End of function getchildren

End of class NodeIterator.

Class Node:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D0GfX
function name:  __construct
number of ops:  4
compiled vars:  !0 = $data
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   43     0  E >   RECV                                                 !0      
   45     1        ASSIGN_OBJ                                                   'data'
          2        OP_DATA                                                      !0
   46     3      > RETURN                                                       null

End of function __construct

Function getdata:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D0GfX
function name:  getData
number of ops:  3
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   53     0  E >   FETCH_OBJ_R                                          ~0      'data'
          1      > RETURN                                                       ~0
   54     2*     > RETURN                                                       null

End of function getdata

Function haschildren:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D0GfX
function name:  hasChildren
number of ops:  5
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   61     0  E >   FETCH_OBJ_R                                          ~0      'children'
          1        COUNT                                                ~1      ~0
          2        IS_SMALLER                                           ~2      0, ~1
          3      > RETURN                                                       ~2
   62     4*     > RETURN                                                       null

End of function haschildren

Function getchildren:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D0GfX
function name:  getChildren
number of ops:  7
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   69     0  E >   NEW                                                  $0      'RecursiveArrayIterator'
          1        CHECK_FUNC_ARG                                               
          2        FETCH_OBJ_FUNC_ARG                                   $1      'children'
          3        SEND_FUNC_ARG                                                $1
          4        DO_FCALL                                          0          
          5      > RETURN                                                       $0
   70     6*     > RETURN                                                       null

End of function getchildren

Function addchild:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D0GfX
function name:  addChild
number of ops:  14
compiled vars:  !0 = $child
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   77     0  E >   RECV                                                 !0      
   79     1        FETCH_OBJ_R                                          ~1      'children'
          2        FRAMELESS_ICALL_3                in_array            ~2      !0, ~1
          3        OP_DATA                                                      <true>
          4      > JMPZ                                                         ~2, ->9
   80     5    >   NEW                                                  $3      'InvalidArgumentException'
          6        SEND_VAL_EX                                                  'duplicate'
          7        DO_FCALL                                          0          
          8      > THROW                                             0          $3
   83     9    >   FETCH_OBJ_W                                          $5      'children'
         10        ASSIGN_DIM                                                   $5
         11        OP_DATA                                                      !0
   85    12      > RETURN                                                       !0
   86    13*     > RETURN                                                       null

End of function addchild

Function jsonserialize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D0GfX
function name:  jsonSerialize
number of ops:  6
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   94     0  E >   FETCH_OBJ_R                                          ~0      'data'
          1        INIT_ARRAY                                           ~1      ~0, 'data'
   95     2        FETCH_OBJ_R                                          ~2      'children'
          3        ADD_ARRAY_ELEMENT                                    ~1      ~2, 'children'
          4      > RETURN                                                       ~1
   97     5*     > RETURN                                                       null

End of function jsonserialize

Function getiterator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D0GfX
function name:  getIterator
number of ops:  7
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
  104     0  E >   NEW                                                  $0      'NodeIterator'
          1        FETCH_THIS                                           ~1      
          2        INIT_ARRAY                                           ~2      ~1
          3        SEND_VAL_EX                                                  ~2
          4        DO_FCALL                                          0          
          5      > RETURN                                                       $0
  105     6*     > RETURN                                                       null

End of function getiterator

End of class Node.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
166.7 ms | 3029 KiB | 18 Q