3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* readonly */ class ImmutableCounter { public function __construct(private readonly int $count) {} public function add1(): self { return new self($this->count + 1); } public function value(): int { return $this->count; } } // assuming the proposed RFC is in place, this is now possible: class MutableCounter extends ImmutableCounter { public function __construct(private int $count) {} public function add1(): self { return new self(++$this->count); } public function value(): int { return $this->count; } } $counter1 = new ImmutableCounter(0); $counter2 = $counter1->add1(); $counter3 = $counter2->add1(); var_dump([$counter1->value(), $counter2->value(), $counter3->value()]); $mutableCounter1 = new MutableCounter(0); $mutableCounter2 = $mutableCounter1->add1(); $mutableCounter3 = $mutableCounter2->add1(); var_dump([$mutableCounter1->value(), $mutableCounter2->value(), $mutableCounter3->value()]);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IDhRY
function name:  (null)
number of ops:  45
compiled vars:  !0 = $counter1, !1 = $counter2, !2 = $counter3, !3 = $mutableCounter1, !4 = $mutableCounter2, !5 = $mutableCounter3
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   NEW                                              $6      'ImmutableCounter'
          1        SEND_VAL_EX                                              0
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $6
   21     4        INIT_METHOD_CALL                                         !0, 'add1'
          5        DO_FCALL                                      0  $9      
          6        ASSIGN                                                   !1, $9
   22     7        INIT_METHOD_CALL                                         !1, 'add1'
          8        DO_FCALL                                      0  $11     
          9        ASSIGN                                                   !2, $11
   24    10        INIT_FCALL                                               'var_dump'
         11        INIT_METHOD_CALL                                         !0, 'value'
         12        DO_FCALL                                      0  $13     
         13        INIT_ARRAY                                       ~14     $13
         14        INIT_METHOD_CALL                                         !1, 'value'
         15        DO_FCALL                                      0  $15     
         16        ADD_ARRAY_ELEMENT                                ~14     $15
         17        INIT_METHOD_CALL                                         !2, 'value'
         18        DO_FCALL                                      0  $16     
         19        ADD_ARRAY_ELEMENT                                ~14     $16
         20        SEND_VAL                                                 ~14
         21        DO_ICALL                                                 
   26    22        NEW                                              $18     'MutableCounter'
         23        SEND_VAL_EX                                              0
         24        DO_FCALL                                      0          
         25        ASSIGN                                                   !3, $18
   27    26        INIT_METHOD_CALL                                         !3, 'add1'
         27        DO_FCALL                                      0  $21     
         28        ASSIGN                                                   !4, $21
   28    29        INIT_METHOD_CALL                                         !4, 'add1'
         30        DO_FCALL                                      0  $23     
         31        ASSIGN                                                   !5, $23
   30    32        INIT_FCALL                                               'var_dump'
         33        INIT_METHOD_CALL                                         !3, 'value'
         34        DO_FCALL                                      0  $25     
         35        INIT_ARRAY                                       ~26     $25
         36        INIT_METHOD_CALL                                         !4, 'value'
         37        DO_FCALL                                      0  $27     
         38        ADD_ARRAY_ELEMENT                                ~26     $27
         39        INIT_METHOD_CALL                                         !5, 'value'
         40        DO_FCALL                                      0  $28     
         41        ADD_ARRAY_ELEMENT                                ~26     $28
         42        SEND_VAL                                                 ~26
         43        DO_ICALL                                                 
         44      > RETURN                                                   1

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

End of function __construct

Function add1:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IDhRY
function name:  add1
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   NEW                          self                $0      
          1        FETCH_OBJ_R                                      ~1      'count'
          2        ADD                                              ~2      ~1, 1
          3        SEND_VAL_EX                                              ~2
          4        DO_FCALL                                      0          
          5        VERIFY_RETURN_TYPE                                       $0
          6      > RETURN                                                   $0
          7*       VERIFY_RETURN_TYPE                                       
          8*     > RETURN                                                   null

End of function add1

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

End of function value

End of class ImmutableCounter.

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

End of function __construct

Function add1:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IDhRY
function name:  add1
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   NEW                          self                $0      
          1        PRE_INC_OBJ                                      ~1      'count'
          2        SEND_VAL_EX                                              ~1
          3        DO_FCALL                                      0          
          4        VERIFY_RETURN_TYPE                                       $0
          5      > RETURN                                                   $0
          6*       VERIFY_RETURN_TYPE                                       
          7*     > RETURN                                                   null

End of function add1

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

End of function value

End of class MutableCounter.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
136.93 ms | 1006 KiB | 14 Q