3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Reference { private static $DATA = []; private $index = 0; public function __construct($value) { // cannot create a reference of a reference if ($value instanceof self) { $this->index = $value->index; } else { $this->index = count(self::$DATA); self::$DATA[$this->index] = $value; } } public function get() { return self::$DATA[$this->index]; } public function set($value) { self::$DATA[$this->index] = $value; } } $x = [1, 2, 3]; // foreach($x as &$ref) { ... } /* $x[0] */ $x[0] = new Reference($x[0]); // Reference#0(index 0) = 1 $ref = clone $x[0]; // Reference#1(index 0) $ref->set($ref->get() + 100); // set index 0 = 101 /* $x[1] */ $x[1] = new Reference($x[1]); // Reference#2(index 1) = 2 $ref = clone $x[1]; // Reference#3(index 1) $x[0] = $x[0]->get(); // without $ref, $x[0] is the only reference to its value and demoted back to a value $ref->set($ref->get() + 100); // set index 1 = 101 /* $x[2] */ $x[2] = new Reference($x[2]); // Reference#4(index 2) = 3 $ref = clone $x[2]; // Reference#5(index 2) $x[1] = $x[1]->get(); // demoted $ref->set($ref->get() + 100); // set index 2 = 103 // end foreach /* $x is [101, 102, Reference#4(index 2)] */ /* $ref remains as Reference#5(index 2) */ $b = $x; // $b[0] = clone $x[0]; - except $x[0] is an integer so this was automatic // $b[1] = clone $x[1]; - same $b[2] = clone $x[2]; // creating a copy of what was in $x[2] /* $b is [101, 102, Reference#6(index 2)] - a different Reference as $x[2] and $ref but the same underlying value */ $b[0] = 0; // foreach($x as &$ref) { ... } /* $x[0] */ $x[0] = new Reference($x[0]); // Reference#7(index 3) = 101 $ref = clone $x[0]; // Reference#8(index 3) $ref->set($ref->get() + 100); // set index 3 = 201 /* $x[1] */ $x[1] = new Reference($x[1]); // Reference#9(index 4) = 102 $ref = clone $x[1]; // Reference#10(index 4) $x[0] = $x[0]->get(); // demoted $ref->set($ref->get() + 100); // set index 4 = 202 /* $x[2] */ // $x[2] = new Reference($x[2]); // - except $x[2] is already Reference#4(index 2) $ref = clone $x[2]; // Reference#11(index 2) $x[1] = $x[1]->get(); // demoted $ref->set($ref->get() + 100); // set index 2 = 203 // end foreach /* $x is [201, 202, Reference#4(index 2)] */ /* $ref remains as Reference#11(index 2) */ /* $b was not modified during the foreach and still contains [0, 102, Reference#6(index 2)] */ /* however the underlying value of the Reference did change */ // var_dump($b); var_dump([$b[0], $b[1], $b[2]->get()]);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MrNID
function name:  (null)
number of ops:  129
compiled vars:  !0 = $x, !1 = $ref, !2 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   ASSIGN                                                   !0, <array>
   32     1        NEW                                              $5      'Reference'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_DIM_FUNC_ARG                               $6      !0, 0
          4        SEND_FUNC_ARG                                            $6
          5        DO_FCALL                                      0          
          6        ASSIGN_DIM                                               !0, 0
          7        OP_DATA                                                  $5
   33     8        FETCH_DIM_R                                      ~8      !0, 0
          9        CLONE                                            ~9      ~8
         10        ASSIGN                                                   !1, ~9
   34    11        INIT_METHOD_CALL                                         !1, 'set'
         12        INIT_METHOD_CALL                                         !1, 'get'
         13        DO_FCALL                                      0  $11     
         14        ADD                                              ~12     $11, 100
         15        SEND_VAL_EX                                              ~12
         16        DO_FCALL                                      0          
   36    17        NEW                                              $15     'Reference'
         18        CHECK_FUNC_ARG                                           
         19        FETCH_DIM_FUNC_ARG                               $16     !0, 1
         20        SEND_FUNC_ARG                                            $16
         21        DO_FCALL                                      0          
         22        ASSIGN_DIM                                               !0, 1
         23        OP_DATA                                                  $15
   37    24        FETCH_DIM_R                                      ~18     !0, 1
         25        CLONE                                            ~19     ~18
         26        ASSIGN                                                   !1, ~19
   38    27        FETCH_DIM_R                                      ~22     !0, 0
         28        INIT_METHOD_CALL                                         ~22, 'get'
         29        DO_FCALL                                      0  $23     
         30        ASSIGN_DIM                                               !0, 0
         31        OP_DATA                                                  $23
   39    32        INIT_METHOD_CALL                                         !1, 'set'
         33        INIT_METHOD_CALL                                         !1, 'get'
         34        DO_FCALL                                      0  $24     
         35        ADD                                              ~25     $24, 100
         36        SEND_VAL_EX                                              ~25
         37        DO_FCALL                                      0          
   41    38        NEW                                              $28     'Reference'
         39        CHECK_FUNC_ARG                                           
         40        FETCH_DIM_FUNC_ARG                               $29     !0, 2
         41        SEND_FUNC_ARG                                            $29
         42        DO_FCALL                                      0          
         43        ASSIGN_DIM                                               !0, 2
         44        OP_DATA                                                  $28
   42    45        FETCH_DIM_R                                      ~31     !0, 2
         46        CLONE                                            ~32     ~31
         47        ASSIGN                                                   !1, ~32
   43    48        FETCH_DIM_R                                      ~35     !0, 1
         49        INIT_METHOD_CALL                                         ~35, 'get'
         50        DO_FCALL                                      0  $36     
         51        ASSIGN_DIM                                               !0, 1
         52        OP_DATA                                                  $36
   44    53        INIT_METHOD_CALL                                         !1, 'set'
         54        INIT_METHOD_CALL                                         !1, 'get'
         55        DO_FCALL                                      0  $37     
         56        ADD                                              ~38     $37, 100
         57        SEND_VAL_EX                                              ~38
         58        DO_FCALL                                      0          
   50    59        ASSIGN                                                   !2, !0
   53    60        FETCH_DIM_R                                      ~42     !0, 2
         61        CLONE                                            ~43     ~42
         62        ASSIGN_DIM                                               !2, 2
         63        OP_DATA                                                  ~43
   57    64        ASSIGN_DIM                                               !2, 0
         65        OP_DATA                                                  0
   61    66        NEW                                              $46     'Reference'
         67        CHECK_FUNC_ARG                                           
         68        FETCH_DIM_FUNC_ARG                               $47     !0, 0
         69        SEND_FUNC_ARG                                            $47
         70        DO_FCALL                                      0          
         71        ASSIGN_DIM                                               !0, 0
         72        OP_DATA                                                  $46
   62    73        FETCH_DIM_R                                      ~49     !0, 0
         74        CLONE                                            ~50     ~49
         75        ASSIGN                                                   !1, ~50
   63    76        INIT_METHOD_CALL                                         !1, 'set'
         77        INIT_METHOD_CALL                                         !1, 'get'
         78        DO_FCALL                                      0  $52     
         79        ADD                                              ~53     $52, 100
         80        SEND_VAL_EX                                              ~53
         81        DO_FCALL                                      0          
   65    82        NEW                                              $56     'Reference'
         83        CHECK_FUNC_ARG                                           
         84        FETCH_DIM_FUNC_ARG                               $57     !0, 1
         85        SEND_FUNC_ARG                                            $57
         86        DO_FCALL                                      0          
         87        ASSIGN_DIM                                               !0, 1
         88        OP_DATA                                                  $56
   66    89        FETCH_DIM_R                                      ~59     !0, 1
         90        CLONE                                            ~60     ~59
         91        ASSIGN                                                   !1, ~60
   67    92        FETCH_DIM_R                                      ~63     !0, 0
         93        INIT_METHOD_CALL                                         ~63, 'get'
         94        DO_FCALL                                      0  $64     
         95        ASSIGN_DIM                                               !0, 0
         96        OP_DATA                                                  $64
   68    97        INIT_METHOD_CALL                                         !1, 'set'
         98        INIT_METHOD_CALL                                         !1, 'get'
         99        DO_FCALL                                      0  $65     
        100        ADD                                              ~66     $65, 100
        101        SEND_VAL_EX                                              ~66
        102        DO_FCALL                                      0          
   71   103        FETCH_DIM_R                                      ~68     !0, 2
        104        CLONE                                            ~69     ~68
        105        ASSIGN                                                   !1, ~69
   72   106        FETCH_DIM_R                                      ~72     !0, 1
        107        INIT_METHOD_CALL                                         ~72, 'get'
        108        DO_FCALL                                      0  $73     
        109        ASSIGN_DIM                                               !0, 1
        110        OP_DATA                                                  $73
   73   111        INIT_METHOD_CALL                                         !1, 'set'
        112        INIT_METHOD_CALL                                         !1, 'get'
        113        DO_FCALL                                      0  $74     
        114        ADD                                              ~75     $74, 100
        115        SEND_VAL_EX                                              ~75
        116        DO_FCALL                                      0          
   83   117        INIT_FCALL                                               'var_dump'
        118        FETCH_DIM_R                                      ~77     !2, 0
        119        INIT_ARRAY                                       ~78     ~77
        120        FETCH_DIM_R                                      ~79     !2, 1
        121        ADD_ARRAY_ELEMENT                                ~78     ~79
        122        FETCH_DIM_R                                      ~80     !2, 2
        123        INIT_METHOD_CALL                                         ~80, 'get'
        124        DO_FCALL                                      0  $81     
        125        ADD_ARRAY_ELEMENT                                ~78     $81
        126        SEND_VAL                                                 ~78
        127        DO_ICALL                                                 
        128      > RETURN                                                   1

Class Reference:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MrNID
function name:  __construct
number of ops:  16
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
   10     1        INSTANCEOF                                               !0
          2      > JMPZ                                                     ~1, ->7
   11     3    >   FETCH_OBJ_R                                      ~3      !0, 'index'
          4        ASSIGN_OBJ                                               'index'
          5        OP_DATA                                                  ~3
          6      > JMP                                                      ->15
   13     7    >   FETCH_STATIC_PROP_R          unknown             ~5      'DATA'
          8        COUNT                                            ~6      ~5
          9        ASSIGN_OBJ                                               'index'
         10        OP_DATA                                                  ~6
   14    11        FETCH_OBJ_R                                      ~8      'index'
         12        FETCH_STATIC_PROP_W          unknown             $7      'DATA'
         13        ASSIGN_DIM                                               $7, ~8
         14        OP_DATA                                                  !0
   16    15    > > RETURN                                                   null

End of function __construct

Function get:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MrNID
function name:  get
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   FETCH_OBJ_R                                      ~1      'index'
          1        FETCH_STATIC_PROP_R          unknown             ~0      'DATA'
          2        FETCH_DIM_R                                      ~2      ~0, ~1
          3      > RETURN                                                   ~2
   20     4*     > RETURN                                                   null

End of function get

Function set:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MrNID
function name:  set
number of ops:  6
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
   23     1        FETCH_OBJ_R                                      ~2      'index'
          2        FETCH_STATIC_PROP_W          global              $1      'DATA'
          3        ASSIGN_DIM                                               $1, ~2
          4        OP_DATA                                                  !0
   24     5      > RETURN                                                   null

End of function set

End of class Reference.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
174.21 ms | 1412 KiB | 15 Q