3v4l.org

run code in 300+ PHP versions simultaneously
<?php class State { protected $foo; protected $bar; public function getFoo() { return $this->foo; } public function getBar() { return $this->bar; } public final function setFoo($foo) { $this->foo = $foo; } public final function setBar($bar) { $this->bar = $bar; } public final function transaction(callable $function) { $proxy = new StateProxy($this); if (call_user_func($function, $proxy)) { $proxy->commit(); } unset($proxy); } } class StateProxy extends State { protected $state; public function __construct(State $state) { $this->state = $state; } public function getFoo() { return parent::getFoo() ? : $this->state->getFoo(); } public function getBar() { return parent::getBar() ? : $this->state->getBar(); } public function commit() { $this->state->foo = $this->foo; $this->state->bar = $this->bar; } } $state = new State(); $state->setFoo('abc'); $state->setBar('123'); var_dump($state); // transaction succeeds, state changes $state->transaction(function(StateProxy $proxy) { $proxy->setFoo('def'); $proxy->setBar('456'); return true; }); var_dump($state); // transaction fails, state remains unchanged $state->transaction(function(StateProxy $proxy) { $proxy->setFoo('ghi'); $proxy->setBar('789'); return false; }); var_dump($state); $state = new State(); $state->setFoo('abc'); $state->setBar('123'); $state->transaction(function(StateProxy $proxy) { $proxy->transaction(function(StateProxy $proxy) { $proxy->transaction(function(StateProxy $proxy) { $proxy->transaction(function(StateProxy $proxy) { $proxy->setFoo('def'); $proxy->setBar('456'); return false; }); // this'll be the one to succeed \/ $proxy->transaction(function(StateProxy $proxy) { $proxy->setFoo('ghi'); $proxy->setBar('789'); return true; }); return true; }); $proxy->transaction(function(StateProxy $proxy) { $proxy->setFoo('jkl'); $proxy->setBar('012'); return false; }); return true; }); return true; }); var_dump($state);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cBBFB
function name:  (null)
number of ops:  43
compiled vars:  !0 = $state
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   NEW                                              $1      'State'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   58     3        INIT_METHOD_CALL                                         !0, 'setFoo'
          4        SEND_VAL_EX                                              'abc'
          5        DO_FCALL                                      0          
   59     6        INIT_METHOD_CALL                                         !0, 'setBar'
          7        SEND_VAL_EX                                              '123'
          8        DO_FCALL                                      0          
   60     9        INIT_FCALL                                               'var_dump'
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                                 
   63    12        INIT_METHOD_CALL                                         !0, 'transaction'
         13        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FcBBFB%3A63%240'
   67    14        SEND_VAL_EX                                              ~7
         15        DO_FCALL                                      0          
   68    16        INIT_FCALL                                               'var_dump'
         17        SEND_VAR                                                 !0
         18        DO_ICALL                                                 
   71    19        INIT_METHOD_CALL                                         !0, 'transaction'
         20        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FcBBFB%3A71%241'
   75    21        SEND_VAL_EX                                              ~10
         22        DO_FCALL                                      0          
   76    23        INIT_FCALL                                               'var_dump'
         24        SEND_VAR                                                 !0
         25        DO_ICALL                                                 
   78    26        NEW                                              $13     'State'
         27        DO_FCALL                                      0          
         28        ASSIGN                                                   !0, $13
   79    29        INIT_METHOD_CALL                                         !0, 'setFoo'
         30        SEND_VAL_EX                                              'abc'
         31        DO_FCALL                                      0          
   80    32        INIT_METHOD_CALL                                         !0, 'setBar'
         33        SEND_VAL_EX                                              '123'
         34        DO_FCALL                                      0          
   82    35        INIT_METHOD_CALL                                         !0, 'transaction'
         36        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FcBBFB%3A82%242'
  107    37        SEND_VAL_EX                                              ~18
         38        DO_FCALL                                      0          
  108    39        INIT_FCALL                                               'var_dump'
         40        SEND_VAR                                                 !0
         41        DO_ICALL                                                 
         42      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FcBBFB%3A63%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cBBFB
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $proxy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
   64     1        INIT_METHOD_CALL                                         !0, 'setFoo'
          2        SEND_VAL_EX                                              'def'
          3        DO_FCALL                                      0          
   65     4        INIT_METHOD_CALL                                         !0, 'setBar'
          5        SEND_VAL_EX                                              '456'
          6        DO_FCALL                                      0          
   66     7      > RETURN                                                   <true>
   67     8*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FcBBFB%3A63%240

Function %00%7Bclosure%7D%2Fin%2FcBBFB%3A71%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cBBFB
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $proxy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   RECV                                             !0      
   72     1        INIT_METHOD_CALL                                         !0, 'setFoo'
          2        SEND_VAL_EX                                              'ghi'
          3        DO_FCALL                                      0          
   73     4        INIT_METHOD_CALL                                         !0, 'setBar'
          5        SEND_VAL_EX                                              '789'
          6        DO_FCALL                                      0          
   74     7      > RETURN                                                   <false>
   75     8*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FcBBFB%3A71%241

Function %00%7Bclosure%7D%2Fin%2FcBBFB%3A82%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cBBFB
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $proxy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   82     0  E >   RECV                                             !0      
   83     1        INIT_METHOD_CALL                                         !0, 'transaction'
          2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FcBBFB%3A83%243'
  105     3        SEND_VAL_EX                                              ~1
          4        DO_FCALL                                      0          
  106     5      > RETURN                                                   <true>
  107     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FcBBFB%3A82%242

Function %00%7Bclosure%7D%2Fin%2FcBBFB%3A83%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cBBFB
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $proxy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   RECV                                             !0      
   84     1        INIT_METHOD_CALL                                         !0, 'transaction'
          2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FcBBFB%3A84%244'
   98     3        SEND_VAL_EX                                              ~1
          4        DO_FCALL                                      0          
   99     5        INIT_METHOD_CALL                                         !0, 'transaction'
          6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FcBBFB%3A99%247'
  103     7        SEND_VAL_EX                                              ~3
          8        DO_FCALL                                      0          
  104     9      > RETURN                                                   <true>
  105    10*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FcBBFB%3A83%243

Function %00%7Bclosure%7D%2Fin%2FcBBFB%3A84%244:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cBBFB
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $proxy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   84     0  E >   RECV                                             !0      
   85     1        INIT_METHOD_CALL                                         !0, 'transaction'
          2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FcBBFB%3A85%245'
   89     3        SEND_VAL_EX                                              ~1
          4        DO_FCALL                                      0          
   92     5        INIT_METHOD_CALL                                         !0, 'transaction'
          6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FcBBFB%3A92%246'
   96     7        SEND_VAL_EX                                              ~3
          8        DO_FCALL                                      0          
   97     9      > RETURN                                                   <true>
   98    10*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FcBBFB%3A84%244

Function %00%7Bclosure%7D%2Fin%2FcBBFB%3A85%245:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cBBFB
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $proxy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   85     0  E >   RECV                                             !0      
   86     1        INIT_METHOD_CALL                                         !0, 'setFoo'
          2        SEND_VAL_EX                                              'def'
          3        DO_FCALL                                      0          
   87     4        INIT_METHOD_CALL                                         !0, 'setBar'
          5        SEND_VAL_EX                                              '456'
          6        DO_FCALL                                      0          
   88     7      > RETURN                                                   <false>
   89     8*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FcBBFB%3A85%245

Function %00%7Bclosure%7D%2Fin%2FcBBFB%3A92%246:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cBBFB
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $proxy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   92     0  E >   RECV                                             !0      
   93     1        INIT_METHOD_CALL                                         !0, 'setFoo'
          2        SEND_VAL_EX                                              'ghi'
          3        DO_FCALL                                      0          
   94     4        INIT_METHOD_CALL                                         !0, 'setBar'
          5        SEND_VAL_EX                                              '789'
          6        DO_FCALL                                      0          
   95     7      > RETURN                                                   <true>
   96     8*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FcBBFB%3A92%246

Function %00%7Bclosure%7D%2Fin%2FcBBFB%3A99%247:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cBBFB
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $proxy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   99     0  E >   RECV                                             !0      
  100     1        INIT_METHOD_CALL                                         !0, 'setFoo'
          2        SEND_VAL_EX                                              'jkl'
          3        DO_FCALL                                      0          
  101     4        INIT_METHOD_CALL                                         !0, 'setBar'
          5        SEND_VAL_EX                                              '012'
          6        DO_FCALL                                      0          
  102     7      > RETURN                                                   <false>
  103     8*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FcBBFB%3A99%247

Class State:
Function getfoo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cBBFB
function name:  getFoo
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   FETCH_OBJ_R                                      ~0      'foo'
          1      > RETURN                                                   ~0
   10     2*     > RETURN                                                   null

End of function getfoo

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

End of function getbar

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

End of function setfoo

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

End of function setbar

Function transaction:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/cBBFB
function name:  transaction
number of ops:  14
compiled vars:  !0 = $function, !1 = $proxy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        NEW                                              $2      'StateProxy'
          2        FETCH_THIS                                       $3      
          3        SEND_VAR_EX                                              $3
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !1, $2
   26     6        INIT_USER_CALL                                1          'call_user_func', !0
          7        SEND_USER                                                !1
          8        DO_FCALL                                      0  $6      
          9      > JMPZ                                                     $6, ->12
   27    10    >   INIT_METHOD_CALL                                         !1, 'commit'
         11        DO_FCALL                                      0          
   29    12    >   UNSET_CV                                                 !1
   30    13      > RETURN                                                   null

End of function transaction

End of class State.

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

End of function __construct

Function getfoo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cBBFB
function name:  getFoo
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   INIT_STATIC_METHOD_CALL                                  'getFoo'
          1        DO_FCALL                                      0  $0      
          2        JMP_SET                                          ~1      $0, ->7
          3        FETCH_OBJ_R                                      ~2      'state'
          4        INIT_METHOD_CALL                                         ~2, 'getFoo'
          5        DO_FCALL                                      0  $3      
          6        QM_ASSIGN                                        ~1      $3
          7      > RETURN                                                   ~1
   44     8*     > RETURN                                                   null

End of function getfoo

Function getbar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cBBFB
function name:  getBar
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   INIT_STATIC_METHOD_CALL                                  'getBar'
          1        DO_FCALL                                      0  $0      
          2        JMP_SET                                          ~1      $0, ->7
          3        FETCH_OBJ_R                                      ~2      'state'
          4        INIT_METHOD_CALL                                         ~2, 'getBar'
          5        DO_FCALL                                      0  $3      
          6        QM_ASSIGN                                        ~1      $3
          7      > RETURN                                                   ~1
   48     8*     > RETURN                                                   null

End of function getbar

Function commit:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cBBFB
function name:  commit
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   FETCH_OBJ_R                                      ~2      'foo'
          1        FETCH_OBJ_W                                      $0      'state'
          2        ASSIGN_OBJ                                               $0, 'foo'
          3        OP_DATA                                                  ~2
   52     4        FETCH_OBJ_R                                      ~5      'bar'
          5        FETCH_OBJ_W                                      $3      'state'
          6        ASSIGN_OBJ                                               $3, 'bar'
          7        OP_DATA                                                  ~5
   53     8      > RETURN                                                   null

End of function commit

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

End of function setfoo

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

End of function setbar

Function transaction:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/cBBFB
function name:  transaction
number of ops:  14
compiled vars:  !0 = $function, !1 = $proxy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        NEW                                              $2      'StateProxy'
          2        FETCH_THIS                                       $3      
          3        SEND_VAR_EX                                              $3
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !1, $2
   26     6        INIT_USER_CALL                                1          'call_user_func', !0
          7        SEND_USER                                                !1
          8        DO_FCALL                                      0  $6      
          9      > JMPZ                                                     $6, ->12
   27    10    >   INIT_METHOD_CALL                                         !1, 'commit'
         11        DO_FCALL                                      0          
   29    12    >   UNSET_CV                                                 !1
   30    13      > RETURN                                                   null

End of function transaction

End of class StateProxy.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.77 ms | 1420 KiB | 15 Q