3v4l.org

run code in 300+ PHP versions simultaneously
<?php class OrderItem { public int $order_id; public string $sku; public int $quantity; } class OrderItems { static function get(int $order_id):array { static $times = 0; if ( 0 !== $times ) { trigger_error('If this happens then the second call to callAndSet() evaluated the closure again.'); $times = 1; } // Simulating a two item order $item = new OrderItem(); $item->order_id = $order_id; $item->sku = 'ABC-123'; $item->quantity = 12; $item2 = new OrderItem(); $item2->order_id = $order_id; $item2->sku = 'XYZ-987'; $item2->quantity = 5; return [$item,$item2]; } } function callAndSet(mixed &$lazy) { return $lazy instanceof Closure ? $lazy = $lazy() : $lazy; } class Order { public int $order_id; public Closure|array $order_items; function __construct(int $order_id) { $this->order_id = $order_id; $this->order_items = fn() => OrderItems::get($order_id); } } $order = new Order(123); foreach( callAndSet($order->order_items) as $order_item) { printf("%d of %s\n", $order_item->quantity, $order_item->sku); } echo "---\n"; foreach( callAndSet($order->order_items) as $order_item) { printf("%d of %s\n", $order_item->quantity, $order_item->sku); } echo "---\n"; foreach( $order->order_items as $order_item) { printf("%d of %s\n", $order_item->quantity, $order_item->sku); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 18
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 18
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 18
2 jumps found. (Code = 77) Position 1 = 25, Position 2 = 34
Branch analysis from position: 25
2 jumps found. (Code = 78) Position 1 = 26, Position 2 = 34
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 34
2 jumps found. (Code = 77) Position 1 = 38, Position 2 = 47
Branch analysis from position: 38
2 jumps found. (Code = 78) Position 1 = 39, Position 2 = 47
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 47
Branch analysis from position: 34
Branch analysis from position: 18
filename:       /in/7rs3N
function name:  (null)
number of ops:  49
compiled vars:  !0 = $order, !1 = $order_item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   NEW                                              $2      'Order'
          1        SEND_VAL_EX                                              123
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $2
   42     4        INIT_FCALL                                               'callandset'
          5        FETCH_OBJ_W                                      $5      !0, 'order_items'
          6        SEND_REF                                                 $5
          7        DO_FCALL                                      0  $6      
          8      > FE_RESET_R                                       $7      $6, ->18
          9    > > FE_FETCH_R                                               $7, !1, ->18
   43    10    >   INIT_FCALL                                               'printf'
         11        SEND_VAL                                                 '%25d+of+%25s%0A'
   44    12        FETCH_OBJ_R                                      ~8      !1, 'quantity'
         13        SEND_VAL                                                 ~8
   45    14        FETCH_OBJ_R                                      ~9      !1, 'sku'
         15        SEND_VAL                                                 ~9
   43    16        DO_ICALL                                                 
   42    17      > JMP                                                      ->9
         18    >   FE_FREE                                                  $7
   47    19        ECHO                                                     '---%0A'
   48    20        INIT_FCALL                                               'callandset'
         21        FETCH_OBJ_W                                      $11     !0, 'order_items'
         22        SEND_REF                                                 $11
         23        DO_FCALL                                      0  $12     
         24      > FE_RESET_R                                       $13     $12, ->34
         25    > > FE_FETCH_R                                               $13, !1, ->34
   49    26    >   INIT_FCALL                                               'printf'
         27        SEND_VAL                                                 '%25d+of+%25s%0A'
   50    28        FETCH_OBJ_R                                      ~14     !1, 'quantity'
         29        SEND_VAL                                                 ~14
   51    30        FETCH_OBJ_R                                      ~15     !1, 'sku'
         31        SEND_VAL                                                 ~15
   49    32        DO_ICALL                                                 
   48    33      > JMP                                                      ->25
         34    >   FE_FREE                                                  $13
   53    35        ECHO                                                     '---%0A'
   54    36        FETCH_OBJ_R                                      ~17     !0, 'order_items'
         37      > FE_RESET_R                                       $18     ~17, ->47
         38    > > FE_FETCH_R                                               $18, !1, ->47
   55    39    >   INIT_FCALL                                               'printf'
         40        SEND_VAL                                                 '%25d+of+%25s%0A'
   56    41        FETCH_OBJ_R                                      ~19     !1, 'quantity'
         42        SEND_VAL                                                 ~19
   57    43        FETCH_OBJ_R                                      ~20     !1, 'sku'
         44        SEND_VAL                                                 ~20
   55    45        DO_ICALL                                                 
   54    46      > JMP                                                      ->38
         47    >   FE_FREE                                                  $18
   58    48      > RETURN                                                   1

Function callandset:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7rs3N
function name:  callAndSet
number of ops:  11
compiled vars:  !0 = $lazy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   28     1        INSTANCEOF                                               !0, 'Closure'
          2      > JMPZ                                                     ~1, ->8
   29     3    >   INIT_DYNAMIC_CALL                                        !0
          4        DO_FCALL                                      0  $2      
          5        ASSIGN                                           ~3      !0, $2
          6        QM_ASSIGN                                        ~4      ~3
          7      > JMP                                                      ->9
   30     8    >   QM_ASSIGN                                        ~4      !0
          9    > > RETURN                                                   ~4
   31    10*     > RETURN                                                   null

End of function callandset

Class OrderItem: [no user functions]
Class OrderItems:
Function get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/7rs3N
function name:  get
number of ops:  32
compiled vars:  !0 = $order_id, !1 = $times, !2 = $item, !3 = $item2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   10     1        BIND_STATIC                                              !1
   11     2        IS_NOT_IDENTICAL                                         !1, 0
          3      > JMPZ                                                     ~4, ->8
   12     4    >   INIT_FCALL                                               'trigger_error'
          5        SEND_VAL                                                 'If+this+happens+then+the+second+call+to+callAndSet%28%29+evaluated+the+closure+again.'
          6        DO_ICALL                                                 
   13     7        ASSIGN                                                   !1, 1
   16     8    >   NEW                                              $7      'OrderItem'
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !2, $7
   17    11        ASSIGN_OBJ                                               !2, 'order_id'
         12        OP_DATA                                                  !0
   18    13        ASSIGN_OBJ                                               !2, 'sku'
         14        OP_DATA                                                  'ABC-123'
   19    15        ASSIGN_OBJ                                               !2, 'quantity'
         16        OP_DATA                                                  12
   20    17        NEW                                              $13     'OrderItem'
         18        DO_FCALL                                      0          
         19        ASSIGN                                                   !3, $13
   21    20        ASSIGN_OBJ                                               !3, 'order_id'
         21        OP_DATA                                                  !0
   22    22        ASSIGN_OBJ                                               !3, 'sku'
         23        OP_DATA                                                  'XYZ-987'
   23    24        ASSIGN_OBJ                                               !3, 'quantity'
         25        OP_DATA                                                  5
   24    26        INIT_ARRAY                                       ~19     !2
         27        ADD_ARRAY_ELEMENT                                ~19     !3
         28        VERIFY_RETURN_TYPE                                       ~19
         29      > RETURN                                                   ~19
   25    30*       VERIFY_RETURN_TYPE                                       
         31*     > RETURN                                                   null

End of function get

End of class OrderItems.

Class Order:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7rs3N
function name:  __construct
number of ops:  8
compiled vars:  !0 = $order_id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   37     1        ASSIGN_OBJ                                               'order_id'
          2        OP_DATA                                                  !0
   38     3        DECLARE_LAMBDA_FUNCTION                          ~3      [0]
          4        BIND_LEXICAL                                             ~3, !0
          5        ASSIGN_OBJ                                               'order_items'
          6        OP_DATA                                                  ~3
   39     7      > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7rs3N
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $order_id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   BIND_STATIC                                              !0
          1        INIT_STATIC_METHOD_CALL                                  'OrderItems', 'get'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $1      
          4      > RETURN                                                   $1
          5*     > RETURN                                                   null

End of Dynamic Function 0

End of function __construct

End of class Order.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.1 ms | 1459 KiB | 17 Q