3v4l.org

run code in 300+ PHP versions simultaneously
<?php $category = function(string $name, $parent = null) { return new class($name, $parent) { public $name; public $parent; public function __construct(string $name, $parent = null) { $this->name = $name; $this->parent = $parent; } public function breadcrumbs(array $trail = []) { $trail[] = $this->name; return $this->parent ? $this->parent->breadcrumbs($trail) : $trail; } }; }; $product = function(string $name, ...$categories) use($category) { $categories = array_reduce(array_reverse($categories), function($set, $item) use($category) { if (!$set) { return $category($item); } return $category($item, $set); }); return new class($name, $categories) { public $name; public $categories; public function __construct(string $name, $categories) { $this->name = $name; $this->categories = $categories; } public function trail(string $separator = ' / '): string { return implode($separator, $this->categories->breadcrumbs()); } }; }; $book = $product('Harry Potter', 'books', 'hardback', 'young-adult'); $shoes = $product('Air Jordans', 'shoes', 'mens', 'sports', 'basketball'); $tapeMeasure = $product('Stanley 25', 'hardware', 'tools', 'measurement'); var_dump( $book->trail(), // books / hardback / young-adult $shoes->trail(), // shoes / mens / sports / basketball $tapeMeasure->trail() // hardware / tools / measurement );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FNKZI
function name:  (null)
number of ops:  39
compiled vars:  !0 = $category, !1 = $product, !2 = $book, !3 = $shoes, !4 = $tapeMeasure
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_LAMBDA_FUNCTION                          ~5      [0]
          1        ASSIGN                                                   !0, ~5
   21     2        DECLARE_LAMBDA_FUNCTION                          ~7      [1]
          3        BIND_LEXICAL                                             ~7, !0
          4        ASSIGN                                                   !1, ~7
   45     5        INIT_DYNAMIC_CALL                                        !1
          6        SEND_VAL_EX                                              'Harry+Potter'
          7        SEND_VAL_EX                                              'books'
          8        SEND_VAL_EX                                              'hardback'
          9        SEND_VAL_EX                                              'young-adult'
         10        DO_FCALL                                      0  $9      
         11        ASSIGN                                                   !2, $9
   46    12        INIT_DYNAMIC_CALL                                        !1
         13        SEND_VAL_EX                                              'Air+Jordans'
         14        SEND_VAL_EX                                              'shoes'
         15        SEND_VAL_EX                                              'mens'
         16        SEND_VAL_EX                                              'sports'
         17        SEND_VAL_EX                                              'basketball'
         18        DO_FCALL                                      0  $11     
         19        ASSIGN                                                   !3, $11
   47    20        INIT_DYNAMIC_CALL                                        !1
         21        SEND_VAL_EX                                              'Stanley+25'
         22        SEND_VAL_EX                                              'hardware'
         23        SEND_VAL_EX                                              'tools'
         24        SEND_VAL_EX                                              'measurement'
         25        DO_FCALL                                      0  $13     
         26        ASSIGN                                                   !4, $13
   49    27        INIT_FCALL                                               'var_dump'
   50    28        INIT_METHOD_CALL                                         !2, 'trail'
         29        DO_FCALL                                      0  $15     
         30        SEND_VAR                                                 $15
   51    31        INIT_METHOD_CALL                                         !3, 'trail'
         32        DO_FCALL                                      0  $16     
         33        SEND_VAR                                                 $16
   52    34        INIT_METHOD_CALL                                         !4, 'trail'
         35        DO_FCALL                                      0  $17     
         36        SEND_VAR                                                 $17
   49    37        DO_ICALL                                                 
   53    38      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FNKZI
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $name, !1 = $parent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
    4     2        DECLARE_ANON_CLASS                               96      
          3        NEW                                              $3      $2
          4        SEND_VAR_EX                                              !0
          5        SEND_VAR_EX                                              !1
          6        DO_FCALL                                      0          
          7      > RETURN                                                   $3
   19     8*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FNKZI
function name:  {closure}
number of ops:  20
compiled vars:  !0 = $name, !1 = $categories, !2 = $category
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
          1        RECV_VARIADIC                                    !1      
          2        BIND_STATIC                                              !2
   22     3        INIT_FCALL                                               'array_reduce'
          4        INIT_FCALL                                               'array_reverse'
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $3      
          7        SEND_VAR                                                 $3
          8        DECLARE_LAMBDA_FUNCTION                          ~4      [0]
          9        BIND_LEXICAL                                             ~4, !2
   28    10        SEND_VAL                                                 ~4
   22    11        DO_ICALL                                         $5      
         12        ASSIGN                                                   !1, $5
   30    13        DECLARE_ANON_CLASS                               <unknown> 
         14        NEW                                              $8      $7
         15        SEND_VAR_EX                                              !0
         16        SEND_VAR_EX                                              !1
         17        DO_FCALL                                      0          
         18      > RETURN                                                   $8
   43    19*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
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 = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FNKZI
function name:  {closure}
number of ops:  15
compiled vars:  !0 = $set, !1 = $item, !2 = $category
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
   23     3        BOOL_NOT                                         ~3      !0
          4      > JMPZ                                                     ~3, ->9
   24     5    >   INIT_DYNAMIC_CALL                                        !2
          6        SEND_VAR_EX                                              !1
          7        DO_FCALL                                      0  $4      
          8      > RETURN                                                   $4
   27     9    >   INIT_DYNAMIC_CALL                                        !2
         10        SEND_VAR_EX                                              !1
         11        SEND_VAR_EX                                              !0
         12        DO_FCALL                                      0  $5      
         13      > RETURN                                                   $5
   28    14*     > RETURN                                                   null

End of Dynamic Function 0

End of Dynamic Function 1

Class class@anonymous:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FNKZI
function name:  __construct
number of ops:  7
compiled vars:  !0 = $name, !1 = $parent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
    9     2        ASSIGN_OBJ                                               'name'
          3        OP_DATA                                                  !0
   10     4        ASSIGN_OBJ                                               'parent'
          5        OP_DATA                                                  !1
   11     6      > RETURN                                                   null

End of function __construct

Function breadcrumbs:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 12
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FNKZI
function name:  breadcrumbs
number of ops:  15
compiled vars:  !0 = $trail
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV_INIT                                        !0      <array>
   14     1        FETCH_OBJ_R                                      ~2      'name'
          2        ASSIGN_DIM                                               !0
          3        OP_DATA                                                  ~2
   16     4        FETCH_OBJ_R                                      ~3      'parent'
          5      > JMPZ                                                     ~3, ->12
          6    >   FETCH_OBJ_R                                      ~4      'parent'
          7        INIT_METHOD_CALL                                         ~4, 'breadcrumbs'
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0  $5      
         10        QM_ASSIGN                                        ~6      $5
         11      > JMP                                                      ->13
         12    >   QM_ASSIGN                                        ~6      !0
         13    > > RETURN                                                   ~6
   17    14*     > RETURN                                                   null

End of function breadcrumbs

End of class class@anonymous.

Class class@anonymous:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FNKZI
function name:  __construct
number of ops:  7
compiled vars:  !0 = $name, !1 = $categories
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   35     2        ASSIGN_OBJ                                               'name'
          3        OP_DATA                                                  !0
   36     4        ASSIGN_OBJ                                               'categories'
          5        OP_DATA                                                  !1
   37     6      > RETURN                                                   null

End of function __construct

Function trail:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FNKZI
function name:  trail
number of ops:  12
compiled vars:  !0 = $separator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV_INIT                                        !0      '+%2F+'
   40     1        INIT_FCALL                                               'implode'
          2        SEND_VAR                                                 !0
          3        FETCH_OBJ_R                                      ~1      'categories'
          4        INIT_METHOD_CALL                                         ~1, 'breadcrumbs'
          5        DO_FCALL                                      0  $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                         $3      
          8        VERIFY_RETURN_TYPE                                       $3
          9      > RETURN                                                   $3
   41    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null

End of function trail

End of class class@anonymous.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.29 ms | 1014 KiB | 17 Q