3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ImmutableObject { private $property; public function __construct($property) { $this->property = $property; } public function withProperty($property) { $clone = clone($this); $clone->property = $property; return $clone; } public function getProperty() { return $this->property; } } class BrokenImmutableObject { private $property; public function __construct($property) { $this->property = $property; } public function withProperty($property) { if ($property == $this->property) { return $this; } $clone = clone($this); $clone->property = $property; return $clone; } public function getProperty() { return $this->property; } } function it($m,$p){echo"\033[3",$p?'2m✔︎':'1m✘'.register_shutdown_function(function(){die(1);}),"\033[0m It $m\n";} foreach (array('ImmutableObject', 'BrokenImmutableObject') as $class) { print "\n".$class."\n".str_repeat('=', strlen($class))."\n\n"; // Expectation, $start will always have value 'start' $start = new $class('start'); // Expectation, $next is an immutable copy of $start with value 'start' $next = $start->withProperty('start'); // Expectation, $end is an immutable copy of $start with value 'end' $end = $start->withProperty('end'); it('has the value "start" (has value "'.$start->getProperty().'")', 'start' === $start->getProperty()); it('has the value "start" (has value "'.$next->getProperty().'")', 'start' === $next->getProperty()); it('has the value "end" (has value "'.$end->getProperty().'")', 'end' === $end->getProperty()); it('is its own object: start', ! in_array($start, array($next, $end), true)); it('is its own object: next', ! in_array($next, array($start, $end), true)); it('is its own object: end', ! in_array($end, array($start, $next), true)); print "\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 1, Position 2 = 96
Branch analysis from position: 1
2 jumps found. (Code = 78) Position 1 = 2, Position 2 = 96
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 1
Branch analysis from position: 1
Branch analysis from position: 96
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 96
filename:       /in/hre5O
function name:  (null)
number of ops:  98
compiled vars:  !0 = $class, !1 = $start, !2 = $next, !3 = $end
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E > > FE_RESET_R                                       $4      <array>, ->96
          1    > > FE_FETCH_R                                               $4, !0, ->96
   50     2    >   CONCAT                                           ~5      '%0A', !0
          3        CONCAT                                           ~6      ~5, '%0A'
          4        INIT_FCALL                                               'str_repeat'
          5        SEND_VAL                                                 '%3D'
          6        STRLEN                                           ~7      !0
          7        SEND_VAL                                                 ~7
          8        DO_ICALL                                         $8      
          9        CONCAT                                           ~9      ~6, $8
         10        CONCAT                                           ~10     ~9, '%0A%0A'
         11        ECHO                                                     ~10
   53    12        FETCH_CLASS                                   0  $11     !0
         13        NEW                                              $12     $11
         14        SEND_VAL_EX                                              'start'
         15        DO_FCALL                                      0          
         16        ASSIGN                                                   !1, $12
   56    17        INIT_METHOD_CALL                                         !1, 'withProperty'
         18        SEND_VAL_EX                                              'start'
         19        DO_FCALL                                      0  $15     
         20        ASSIGN                                                   !2, $15
   59    21        INIT_METHOD_CALL                                         !1, 'withProperty'
         22        SEND_VAL_EX                                              'end'
         23        DO_FCALL                                      0  $17     
         24        ASSIGN                                                   !3, $17
   61    25        INIT_FCALL                                               'it'
         26        INIT_METHOD_CALL                                         !1, 'getProperty'
         27        DO_FCALL                                      0  $19     
         28        CONCAT                                           ~20     'has+the+value+%22start%22+%28has+value+%22', $19
         29        CONCAT                                           ~21     ~20, '%22%29'
         30        SEND_VAL                                                 ~21
         31        INIT_METHOD_CALL                                         !1, 'getProperty'
         32        DO_FCALL                                      0  $22     
         33        IS_IDENTICAL                                     ~23     $22, 'start'
         34        SEND_VAL                                                 ~23
         35        DO_FCALL                                      0          
   62    36        INIT_FCALL                                               'it'
         37        INIT_METHOD_CALL                                         !2, 'getProperty'
         38        DO_FCALL                                      0  $25     
         39        CONCAT                                           ~26     'has+the+value+%22start%22+%28has+value+%22', $25
         40        CONCAT                                           ~27     ~26, '%22%29'
         41        SEND_VAL                                                 ~27
         42        INIT_METHOD_CALL                                         !2, 'getProperty'
         43        DO_FCALL                                      0  $28     
         44        IS_IDENTICAL                                     ~29     $28, 'start'
         45        SEND_VAL                                                 ~29
         46        DO_FCALL                                      0          
   63    47        INIT_FCALL                                               'it'
         48        INIT_METHOD_CALL                                         !3, 'getProperty'
         49        DO_FCALL                                      0  $31     
         50        CONCAT                                           ~32     'has+the+value+%22end%22+%28has+value+%22', $31
         51        CONCAT                                           ~33     ~32, '%22%29'
         52        SEND_VAL                                                 ~33
         53        INIT_METHOD_CALL                                         !3, 'getProperty'
         54        DO_FCALL                                      0  $34     
         55        IS_IDENTICAL                                     ~35     $34, 'end'
         56        SEND_VAL                                                 ~35
         57        DO_FCALL                                      0          
   65    58        INIT_FCALL                                               'it'
         59        SEND_VAL                                                 'is+its+own+object%3A+start'
         60        INIT_FCALL                                               'in_array'
         61        SEND_VAR                                                 !1
         62        INIT_ARRAY                                       ~37     !2
         63        ADD_ARRAY_ELEMENT                                ~37     !3
         64        SEND_VAL                                                 ~37
         65        SEND_VAL                                                 <true>
         66        DO_ICALL                                         $38     
         67        BOOL_NOT                                         ~39     $38
         68        SEND_VAL                                                 ~39
         69        DO_FCALL                                      0          
   66    70        INIT_FCALL                                               'it'
         71        SEND_VAL                                                 'is+its+own+object%3A+next'
         72        INIT_FCALL                                               'in_array'
         73        SEND_VAR                                                 !2
         74        INIT_ARRAY                                       ~41     !1
         75        ADD_ARRAY_ELEMENT                                ~41     !3
         76        SEND_VAL                                                 ~41
         77        SEND_VAL                                                 <true>
         78        DO_ICALL                                         $42     
         79        BOOL_NOT                                         ~43     $42
         80        SEND_VAL                                                 ~43
         81        DO_FCALL                                      0          
   67    82        INIT_FCALL                                               'it'
         83        SEND_VAL                                                 'is+its+own+object%3A+end'
         84        INIT_FCALL                                               'in_array'
         85        SEND_VAR                                                 !3
         86        INIT_ARRAY                                       ~45     !1
         87        ADD_ARRAY_ELEMENT                                ~45     !2
         88        SEND_VAL                                                 ~45
         89        SEND_VAL                                                 <true>
         90        DO_ICALL                                         $46     
         91        BOOL_NOT                                         ~47     $46
         92        SEND_VAL                                                 ~47
         93        DO_FCALL                                      0          
   69    94        ECHO                                                     '%0A'
   49    95      > JMP                                                      ->1
         96    >   FE_FREE                                                  $4
   70    97      > RETURN                                                   1

Function it:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/hre5O
function name:  it
number of ops:  18
compiled vars:  !0 = $m, !1 = $p
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        ECHO                                                     '%1B%5B3'
          3      > JMPZ                                                     !1, ->6
          4    >   QM_ASSIGN                                        ~2      '2m%E2%9C%94%EF%B8%8E'
          5      > JMP                                                      ->12
          6    >   INIT_FCALL                                               'register_shutdown_function'
          7        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fhre5O%3A47%240'
          8        SEND_VAL                                                 ~3
          9        DO_ICALL                                         $4      
         10        CONCAT                                           ~5      '1m%E2%9C%98', $4
         11        QM_ASSIGN                                        ~2      ~5
         12    >   ECHO                                                     ~2
         13        ROPE_INIT                                     3  ~7      '%1B%5B0m+It+'
         14        ROPE_ADD                                      1  ~7      ~7, !0
         15        ROPE_END                                      2  ~6      ~7, '%0A'
         16        ECHO                                                     ~6
         17      > RETURN                                                   null

End of function it

Function %00%7Bclosure%7D%2Fin%2Fhre5O%3A47%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 79) Position 1 = -2
filename:       /in/hre5O
function name:  {closure}
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
          0  E > > EXIT                                                     1
          1*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fhre5O%3A47%240

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

End of function __construct

Function withproperty:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/hre5O
function name:  withProperty
number of ops:  8
compiled vars:  !0 = $property, !1 = $clone
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
   12     1        FETCH_THIS                                       ~2      
          2        CLONE                                            ~3      ~2
          3        ASSIGN                                                   !1, ~3
   13     4        ASSIGN_OBJ                                               !1, 'property'
          5        OP_DATA                                                  !0
   15     6      > RETURN                                                   !1
   16     7*     > RETURN                                                   null

End of function withproperty

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

End of function getproperty

End of class ImmutableObject.

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

End of function __construct

Function withproperty:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/hre5O
function name:  withProperty
number of ops:  13
compiled vars:  !0 = $property, !1 = $clone
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   32     1        FETCH_OBJ_R                                      ~2      'property'
          2        IS_EQUAL                                                 !0, ~2
          3      > JMPZ                                                     ~3, ->6
   33     4    >   FETCH_THIS                                       ~4      
          5      > RETURN                                                   ~4
   36     6    >   FETCH_THIS                                       ~5      
          7        CLONE                                            ~6      ~5
          8        ASSIGN                                                   !1, ~6
   37     9        ASSIGN_OBJ                                               !1, 'property'
         10        OP_DATA                                                  !0
   39    11      > RETURN                                                   !1
   40    12*     > RETURN                                                   null

End of function withproperty

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

End of function getproperty

End of class BrokenImmutableObject.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
167.71 ms | 1415 KiB | 25 Q