3v4l.org

run code in 300+ PHP versions simultaneously
<?php header("content-type: text/plain"); class WatchVar { private $data = array(); private $org = array(); private $callbacks = array(); public function __set($name, $value) { if (!array_key_exists($name, $this->data)) { $this->org[$name] = $value; } else { //variable gets changed again! $this->triggerChangedEvent($name, $value); } $this->data[$name] = $value; } public function &__get($name) { if (array_key_exists($name, $this->data)) { if ($this->data[$name] != $this->org[$name]) { //variable has changed, return original //return $this->org[$name]; //or return new state: return $this->data[$name]; } else { //variable has not changed return $this->data[$name]; } } } public function addCallback($name, $lambdaFunc) { $this->callbacks[$name] = $lambdaFunc; } protected function triggerChangedEvent($name, $value) { //$this->data[$name] has been changed! //callback call like: call_user_func($this->callbacks[$name], $value); } } $test = new WatchVar; $test->addCallback('xxx', function($newValue) { echo "xxx has changed to {$newValue}\n"; }); $test->xxx = "aaa"; echo $test->xxx . "\n"; //output: aaa $test->xxx = "bbb"; //output: xxx has changed to bbb echo $test->xxx . "\n"; //output bbb function messyFunction(&$var) { $var->xxx = "test"; } messyFunction($test); //output: nothing, why?
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/H0gpF
function name:  (null)
number of ops:  25
compiled vars:  !0 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   INIT_FCALL                                               'header'
          1        SEND_VAL                                                 'content-type%3A+text%2Fplain'
          2        DO_ICALL                                                 
   45     3        NEW                                              $2      'WatchVar'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $2
   46     6        INIT_METHOD_CALL                                         !0, 'addCallback'
          7        SEND_VAL_EX                                              'xxx'
          8        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FH0gpF%3A46%240'
          9        SEND_VAL_EX                                              ~5
         10        DO_FCALL                                      0          
   47    11        ASSIGN_OBJ                                               !0, 'xxx'
         12        OP_DATA                                                  'aaa'
   49    13        FETCH_OBJ_R                                      ~8      !0, 'xxx'
         14        CONCAT                                           ~9      ~8, '%0A'
         15        ECHO                                                     ~9
   52    16        ASSIGN_OBJ                                               !0, 'xxx'
         17        OP_DATA                                                  'bbb'
   55    18        FETCH_OBJ_R                                      ~11     !0, 'xxx'
         19        CONCAT                                           ~12     ~11, '%0A'
         20        ECHO                                                     ~12
   62    21        INIT_FCALL                                               'messyfunction'
         22        SEND_REF                                                 !0
         23        DO_FCALL                                      0          
   63    24      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FH0gpF%3A46%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/H0gpF
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $newValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
          1        ROPE_INIT                                     3  ~2      'xxx+has+changed+to+'
          2        ROPE_ADD                                      1  ~2      ~2, !0
          3        ROPE_END                                      2  ~1      ~2, '%0A'
          4        ECHO                                                     ~1
          5      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FH0gpF%3A46%240

Function messyfunction:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/H0gpF
function name:  messyFunction
number of ops:  4
compiled vars:  !0 = $var
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
   59     1        ASSIGN_OBJ                                               !0, 'xxx'
          2        OP_DATA                                                  'test'
   60     3      > RETURN                                                   null

End of function messyfunction

Class WatchVar:
Function __set:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/H0gpF
function name:  __set
number of ops:  18
compiled vars:  !0 = $name, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   10     2        FETCH_OBJ_R                                      ~2      'data'
          3        ARRAY_KEY_EXISTS                                 ~3      !0, ~2
          4        BOOL_NOT                                         ~4      ~3
          5      > JMPZ                                                     ~4, ->10
   11     6    >   FETCH_OBJ_W                                      $5      'org'
          7        ASSIGN_DIM                                               $5, !0
          8        OP_DATA                                                  !1
          9      > JMP                                                      ->14
   14    10    >   INIT_METHOD_CALL                                         'triggerChangedEvent'
         11        SEND_VAR_EX                                              !0
         12        SEND_VAR_EX                                              !1
         13        DO_FCALL                                      0          
   16    14    >   FETCH_OBJ_W                                      $8      'data'
         15        ASSIGN_DIM                                               $8, !0
         16        OP_DATA                                                  !1
   17    17      > RETURN                                                   null

End of function __set

Function __get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 17
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
Return found
Branch analysis from position: 14
Return found
Branch analysis from position: 17
Return found
filename:       /in/H0gpF
function name:  __get
number of ops:  18
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   20     1        FETCH_OBJ_R                                      ~1      'data'
          2        ARRAY_KEY_EXISTS                                         !0, ~1
          3      > JMPZ                                                     ~2, ->17
   21     4    >   FETCH_OBJ_R                                      ~3      'data'
          5        FETCH_DIM_R                                      ~4      ~3, !0
          6        FETCH_OBJ_R                                      ~5      'org'
          7        FETCH_DIM_R                                      ~6      ~5, !0
          8        IS_NOT_EQUAL                                             ~4, ~6
          9      > JMPZ                                                     ~7, ->14
   25    10    >   FETCH_OBJ_W                                      $8      'data'
         11        FETCH_DIM_W                                      $9      $8, !0
         12      > RETURN_BY_REF                                            $9
         13*       JMP                                                      ->17
   28    14    >   FETCH_OBJ_W                                      $10     'data'
         15        FETCH_DIM_W                                      $11     $10, !0
         16      > RETURN_BY_REF                                            $11
   31    17    > > RETURN_BY_REF                                            null

End of function __get

Function addcallback:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/H0gpF
function name:  addCallback
number of ops:  6
compiled vars:  !0 = $name, !1 = $lambdaFunc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   34     2        FETCH_OBJ_W                                      $2      'callbacks'
          3        ASSIGN_DIM                                               $2, !0
          4        OP_DATA                                                  !1
   35     5      > RETURN                                                   null

End of function addcallback

Function triggerchangedevent:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/H0gpF
function name:  triggerChangedEvent
number of ops:  8
compiled vars:  !0 = $name, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   40     2        FETCH_OBJ_R                                      ~2      'callbacks'
          3        FETCH_DIM_R                                      ~3      ~2, !0
          4        INIT_USER_CALL                                1          'call_user_func', ~3
          5        SEND_USER                                                !1
          6        DO_FCALL                                      0          
   41     7      > RETURN                                                   null

End of function triggerchangedevent

End of class WatchVar.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.8 ms | 1407 KiB | 16 Q