3v4l.org

run code in 300+ PHP versions simultaneously
<?php // from http://stackoverflow.com/a/3764809/4251625 function refcount($var) { ob_start(); debug_zval_dump($var); $dump = ob_get_clean(); $matches = array(); preg_match('/refcount\(([0-9]+)/', $dump, $matches); $count = $matches[1]; //3 references are added, including when calling debug_zval_dump() return $count - 3; } class SomethingBuilder { protected $value; protected $listeners = []; protected function dispatchEvent($evt){ foreach($this->listeners as $listener) $listener($evt); } function addEventListener($listener){ $this->listeners[] = $listener; } function getValue(){ return $this->value; } function setValue($value){ $this->dispatchEvent('beforechange'); $this->value = $value; } function freeze(){ return new SomethingFinalBuilderWrapper($this); } } class SomethingFinalBuilderWrapper { protected $builder; function __construct(SomethingBuilder $builder){ $this->builder = $builder; $this->builder->addEventListener(function($evt){ if($evt !== 'beforechange') return; echo refcount($this->builder); // if(refcount($this->builder) > 1) $this->builder = clone $this->builder; echo "clone happened\n"; }); } function getValue(){ return $this->builder->getValue(); } } $x = new SomethingBuilder(); $x->setValue(10); echo $x->getValue() . "\n"; $y = $x->freeze(); $x->setValue(20); echo $y->getValue() . "\n"; echo $x->getValue() . "\n"; $y = $x->freeze(); $z = $x->freeze(); $x->setValue(20); echo $y->getValue() . "\n"; echo $x->getValue() . "\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0942s
function name:  (null)
number of ops:  42
compiled vars:  !0 = $x, !1 = $y, !2 = $z
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   NEW                                              $3      'SomethingBuilder'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $3
   66     3        INIT_METHOD_CALL                                         !0, 'setValue'
          4        SEND_VAL_EX                                              10
          5        DO_FCALL                                      0          
   67     6        INIT_METHOD_CALL                                         !0, 'getValue'
          7        DO_FCALL                                      0  $7      
          8        CONCAT                                           ~8      $7, '%0A'
          9        ECHO                                                     ~8
   69    10        INIT_METHOD_CALL                                         !0, 'freeze'
         11        DO_FCALL                                      0  $9      
         12        ASSIGN                                                   !1, $9
   71    13        INIT_METHOD_CALL                                         !0, 'setValue'
         14        SEND_VAL_EX                                              20
         15        DO_FCALL                                      0          
   73    16        INIT_METHOD_CALL                                         !1, 'getValue'
         17        DO_FCALL                                      0  $12     
         18        CONCAT                                           ~13     $12, '%0A'
         19        ECHO                                                     ~13
   74    20        INIT_METHOD_CALL                                         !0, 'getValue'
         21        DO_FCALL                                      0  $14     
         22        CONCAT                                           ~15     $14, '%0A'
         23        ECHO                                                     ~15
   76    24        INIT_METHOD_CALL                                         !0, 'freeze'
         25        DO_FCALL                                      0  $16     
         26        ASSIGN                                                   !1, $16
   77    27        INIT_METHOD_CALL                                         !0, 'freeze'
         28        DO_FCALL                                      0  $18     
         29        ASSIGN                                                   !2, $18
   78    30        INIT_METHOD_CALL                                         !0, 'setValue'
         31        SEND_VAL_EX                                              20
         32        DO_FCALL                                      0          
   80    33        INIT_METHOD_CALL                                         !1, 'getValue'
         34        DO_FCALL                                      0  $21     
         35        CONCAT                                           ~22     $21, '%0A'
         36        ECHO                                                     ~22
   81    37        INIT_METHOD_CALL                                         !0, 'getValue'
         38        DO_FCALL                                      0  $23     
         39        CONCAT                                           ~24     $23, '%0A'
         40        ECHO                                                     ~24
         41      > RETURN                                                   1

Function refcount:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0942s
function name:  refcount
number of ops:  20
compiled vars:  !0 = $var, !1 = $dump, !2 = $matches, !3 = $count
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        INIT_FCALL                                               'ob_start'
          2        DO_ICALL                                                 
    6     3        INIT_FCALL                                               'debug_zval_dump'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                                 
    7     6        INIT_FCALL                                               'ob_get_clean'
          7        DO_ICALL                                         $6      
          8        ASSIGN                                                   !1, $6
    9     9        ASSIGN                                                   !2, <array>
   10    10        INIT_FCALL                                               'preg_match'
         11        SEND_VAL                                                 '%2Frefcount%5C%28%28%5B0-9%5D%2B%29%2F'
         12        SEND_VAR                                                 !1
         13        SEND_REF                                                 !2
         14        DO_ICALL                                                 
   12    15        FETCH_DIM_R                                      ~10     !2, 1
         16        ASSIGN                                                   !3, ~10
   15    17        SUB                                              ~12     !3, 3
         18      > RETURN                                                   ~12
   16    19*     > RETURN                                                   null

End of function refcount

Function %00%7Bclosure%7D%2Fin%2F0942s%3A49%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0942s
function name:  {closure}
number of ops:  18
compiled vars:  !0 = $evt
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV                                             !0      
   50     1        IS_NOT_IDENTICAL                                         !0, 'beforechange'
          2      > JMPZ                                                     ~1, ->4
          3    > > RETURN                                                   null
   52     4    >   INIT_FCALL                                               'refcount'
          5        FETCH_THIS                                       $2      
          6        FETCH_OBJ_R                                      ~3      $2, 'builder'
          7        SEND_VAL                                                 ~3
          8        DO_FCALL                                      0  $4      
          9        ECHO                                                     $4
   55    10        FETCH_THIS                                       $5      
         11        FETCH_THIS                                       $7      
         12        FETCH_OBJ_R                                      ~8      $7, 'builder'
         13        CLONE                                            ~9      ~8
         14        ASSIGN_OBJ                                               $5, 'builder'
         15        OP_DATA                                                  ~9
   56    16        ECHO                                                     'clone+happened%0A'
   57    17      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F0942s%3A49%240

Class SomethingBuilder:
Function dispatchevent:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/0942s
function name:  dispatchEvent
number of ops:  10
compiled vars:  !0 = $evt, !1 = $listener
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        FETCH_OBJ_R                                      ~2      'listeners'
          2      > FE_RESET_R                                       $3      ~2, ->8
          3    > > FE_FETCH_R                                               $3, !1, ->8
   26     4    >   INIT_DYNAMIC_CALL                                        !1
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
   25     7      > JMP                                                      ->3
          8    >   FE_FREE                                                  $3
   27     9      > RETURN                                                   null

End of function dispatchevent

Function addeventlistener:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0942s
function name:  addEventListener
number of ops:  5
compiled vars:  !0 = $listener
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
   29     1        FETCH_OBJ_W                                      $1      'listeners'
          2        ASSIGN_DIM                                               $1
          3        OP_DATA                                                  !0
   30     4      > RETURN                                                   null

End of function addeventlistener

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

End of function getvalue

Function setvalue:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0942s
function name:  setValue
number of ops:  7
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   35     1        INIT_METHOD_CALL                                         'dispatchEvent'
          2        SEND_VAL_EX                                              'beforechange'
          3        DO_FCALL                                      0          
   36     4        ASSIGN_OBJ                                               'value'
          5        OP_DATA                                                  !0
   37     6      > RETURN                                                   null

End of function setvalue

Function freeze:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0942s
function name:  freeze
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   NEW                                              $0      'SomethingFinalBuilderWrapper'
          1        FETCH_THIS                                       $1      
          2        SEND_VAR_EX                                              $1
          3        DO_FCALL                                      0          
          4      > RETURN                                                   $0
   40     5*     > RETURN                                                   null

End of function freeze

End of class SomethingBuilder.

Class SomethingFinalBuilderWrapper:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0942s
function name:  __construct
number of ops:  9
compiled vars:  !0 = $builder
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
   48     1        ASSIGN_OBJ                                               'builder'
          2        OP_DATA                                                  !0
   49     3        FETCH_OBJ_R                                      ~2      'builder'
          4        INIT_METHOD_CALL                                         ~2, 'addEventListener'
          5        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F0942s%3A49%240'
   57     6        SEND_VAL_EX                                              ~3
          7        DO_FCALL                                      0          
   58     8      > RETURN                                                   null

End of function __construct

Function getvalue:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0942s
function name:  getValue
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   FETCH_OBJ_R                                      ~0      'builder'
          1        INIT_METHOD_CALL                                         ~0, 'getValue'
          2        DO_FCALL                                      0  $1      
          3      > RETURN                                                   $1
   62     4*     > RETURN                                                   null

End of function getvalue

End of class SomethingFinalBuilderWrapper.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
197.63 ms | 1411 KiB | 22 Q