3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Proxy { private $object; private $onEnter = []; private $onLeave = []; public function __registerOnEnter($method, \Closure $closure) { $this->onEnter[$method][] = $closure; } private function invokeOnEnter($method, array $arguments) { if (empty($this->onEnter[$method])) { return; } foreach ($this->onEnter[$method] as $closure) { call_user_func($closure, $this->object, $arguments); } } public function __registerOnLeave($method, \Closure $closure) { $this->onLeave[$method][] = $closure; } private function invokeOnLeave($method, array $arguments) { if (empty($this->onLeave[$method])) { return; } foreach ($this->onLeave[$method] as $closure) { call_user_func($closure, $this->object, $arguments); } } private function getProxiedMethod($method) { $methodReflection = new \ReflectionMethod($this->object, $method); return $methodReflection->getClosure($this->object)->bindTo($this); } public function __call($method, array $arguments) { $this->invokeOnEnter($method, $arguments); call_user_func_array($this->getProxiedMethod($method), $arguments); $this->invokeOnLeave($method, $arguments); } public function __construct($object) { $this->object = (object) $object; } } class Foo { public function bar() { $this->qux(); } public function qux() { $this->zip(); } public function zip() { var_dump(__CLASS__); } } $fooProxy = new Proxy(new Foo()); $fooProxy->__registerOnEnter('bar', function ($object) { var_dump(sprintf('%s::bar#onEnter', get_class($object))); }); $fooProxy->__registerOnLeave('bar', function ($object) { var_dump(sprintf('%s::bar#onLeave', get_class($object))); }); $fooProxy->__registerOnEnter('qux', function ($object) { var_dump(sprintf('%s::qux#onEnter', get_class($object))); }); $fooProxy->__registerOnLeave('qux', function ($object) { var_dump(sprintf('%s::qux#onLeave', get_class($object))); }); $fooProxy->__registerOnEnter('zip', function ($object) { var_dump(sprintf('%s::zip#onEnter', get_class($object))); }); $fooProxy->__registerOnLeave('zip', function ($object) { var_dump(sprintf('%s::zip#onLeave', get_class($object))); }); $fooProxy->bar();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8I4Tb
function name:  (null)
number of ops:  39
compiled vars:  !0 = $fooProxy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   84     0  E >   NEW                                              $1      'Proxy'
          1        NEW                                              $2      'Foo'
          2        DO_FCALL                                      0          
          3        SEND_VAR_NO_REF_EX                                       $2
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $1
   85     6        INIT_METHOD_CALL                                         !0, '__registerOnEnter'
          7        SEND_VAL_EX                                              'bar'
          8        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F8I4Tb%3A85%240'
   87     9        SEND_VAL_EX                                              ~6
         10        DO_FCALL                                      0          
   88    11        INIT_METHOD_CALL                                         !0, '__registerOnLeave'
         12        SEND_VAL_EX                                              'bar'
         13        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F8I4Tb%3A88%241'
   90    14        SEND_VAL_EX                                              ~8
         15        DO_FCALL                                      0          
   91    16        INIT_METHOD_CALL                                         !0, '__registerOnEnter'
         17        SEND_VAL_EX                                              'qux'
         18        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F8I4Tb%3A91%242'
   93    19        SEND_VAL_EX                                              ~10
         20        DO_FCALL                                      0          
   94    21        INIT_METHOD_CALL                                         !0, '__registerOnLeave'
         22        SEND_VAL_EX                                              'qux'
         23        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F8I4Tb%3A94%243'
   96    24        SEND_VAL_EX                                              ~12
         25        DO_FCALL                                      0          
   97    26        INIT_METHOD_CALL                                         !0, '__registerOnEnter'
         27        SEND_VAL_EX                                              'zip'
         28        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F8I4Tb%3A97%244'
   99    29        SEND_VAL_EX                                              ~14
         30        DO_FCALL                                      0          
  100    31        INIT_METHOD_CALL                                         !0, '__registerOnLeave'
         32        SEND_VAL_EX                                              'zip'
         33        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F8I4Tb%3A100%245'
  102    34        SEND_VAL_EX                                              ~16
         35        DO_FCALL                                      0          
  104    36        INIT_METHOD_CALL                                         !0, 'bar'
         37        DO_FCALL                                      0          
         38      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2F8I4Tb%3A85%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8I4Tb
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   85     0  E >   RECV                                             !0      
   86     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'sprintf'
          3        SEND_VAL                                                 '%25s%3A%3Abar%23onEnter'
          4        GET_CLASS                                        ~1      !0
          5        SEND_VAL                                                 ~1
          6        DO_ICALL                                         $2      
          7        SEND_VAR                                                 $2
          8        DO_ICALL                                                 
   87     9      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F8I4Tb%3A85%240

Function %00%7Bclosure%7D%2Fin%2F8I4Tb%3A88%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8I4Tb
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   RECV                                             !0      
   89     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'sprintf'
          3        SEND_VAL                                                 '%25s%3A%3Abar%23onLeave'
          4        GET_CLASS                                        ~1      !0
          5        SEND_VAL                                                 ~1
          6        DO_ICALL                                         $2      
          7        SEND_VAR                                                 $2
          8        DO_ICALL                                                 
   90     9      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F8I4Tb%3A88%241

Function %00%7Bclosure%7D%2Fin%2F8I4Tb%3A91%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8I4Tb
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   91     0  E >   RECV                                             !0      
   92     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'sprintf'
          3        SEND_VAL                                                 '%25s%3A%3Aqux%23onEnter'
          4        GET_CLASS                                        ~1      !0
          5        SEND_VAL                                                 ~1
          6        DO_ICALL                                         $2      
          7        SEND_VAR                                                 $2
          8        DO_ICALL                                                 
   93     9      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F8I4Tb%3A91%242

Function %00%7Bclosure%7D%2Fin%2F8I4Tb%3A94%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8I4Tb
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   94     0  E >   RECV                                             !0      
   95     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'sprintf'
          3        SEND_VAL                                                 '%25s%3A%3Aqux%23onLeave'
          4        GET_CLASS                                        ~1      !0
          5        SEND_VAL                                                 ~1
          6        DO_ICALL                                         $2      
          7        SEND_VAR                                                 $2
          8        DO_ICALL                                                 
   96     9      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F8I4Tb%3A94%243

Function %00%7Bclosure%7D%2Fin%2F8I4Tb%3A97%244:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8I4Tb
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   97     0  E >   RECV                                             !0      
   98     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'sprintf'
          3        SEND_VAL                                                 '%25s%3A%3Azip%23onEnter'
          4        GET_CLASS                                        ~1      !0
          5        SEND_VAL                                                 ~1
          6        DO_ICALL                                         $2      
          7        SEND_VAR                                                 $2
          8        DO_ICALL                                                 
   99     9      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F8I4Tb%3A97%244

Function %00%7Bclosure%7D%2Fin%2F8I4Tb%3A100%245:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8I4Tb
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  100     0  E >   RECV                                             !0      
  101     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'sprintf'
          3        SEND_VAL                                                 '%25s%3A%3Azip%23onLeave'
          4        GET_CLASS                                        ~1      !0
          5        SEND_VAL                                                 ~1
          6        DO_ICALL                                         $2      
          7        SEND_VAR                                                 $2
          8        DO_ICALL                                                 
  102     9      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F8I4Tb%3A100%245

Class Proxy:
Function __registeronenter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8I4Tb
function name:  __registerOnEnter
number of ops:  7
compiled vars:  !0 = $method, !1 = $closure
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   12     2        FETCH_OBJ_W                                      $2      'onEnter'
          3        FETCH_DIM_W                                      $3      $2, !0
          4        ASSIGN_DIM                                               $3
          5        OP_DATA                                                  !1
   13     6      > RETURN                                                   null

End of function __registeronenter

Function invokeonenter:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 16
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/8I4Tb
function name:  invokeOnEnter
number of ops:  18
compiled vars:  !0 = $method, !1 = $arguments, !2 = $closure
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   17     2        FETCH_OBJ_IS                                     ~3      'onEnter'
          3        ISSET_ISEMPTY_DIM_OBJ                         1          ~3, !0
          4      > JMPZ                                                     ~4, ->6
   19     5    > > RETURN                                                   null
   21     6    >   FETCH_OBJ_R                                      ~5      'onEnter'
          7        FETCH_DIM_R                                      ~6      ~5, !0
          8      > FE_RESET_R                                       $7      ~6, ->16
          9    > > FE_FETCH_R                                               $7, !2, ->16
   23    10    >   INIT_USER_CALL                                2          'call_user_func', !2
         11        FETCH_OBJ_R                                      ~8      'object'
         12        SEND_USER                                                ~8
         13        SEND_USER                                                !1
         14        DO_FCALL                                      0          
   21    15      > JMP                                                      ->9
         16    >   FE_FREE                                                  $7
   25    17      > RETURN                                                   null

End of function invokeonenter

Function __registeronleave:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8I4Tb
function name:  __registerOnLeave
number of ops:  7
compiled vars:  !0 = $method, !1 = $closure
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   29     2        FETCH_OBJ_W                                      $2      'onLeave'
          3        FETCH_DIM_W                                      $3      $2, !0
          4        ASSIGN_DIM                                               $3
          5        OP_DATA                                                  !1
   30     6      > RETURN                                                   null

End of function __registeronleave

Function invokeonleave:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 16
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/8I4Tb
function name:  invokeOnLeave
number of ops:  18
compiled vars:  !0 = $method, !1 = $arguments, !2 = $closure
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   34     2        FETCH_OBJ_IS                                     ~3      'onLeave'
          3        ISSET_ISEMPTY_DIM_OBJ                         1          ~3, !0
          4      > JMPZ                                                     ~4, ->6
   36     5    > > RETURN                                                   null
   38     6    >   FETCH_OBJ_R                                      ~5      'onLeave'
          7        FETCH_DIM_R                                      ~6      ~5, !0
          8      > FE_RESET_R                                       $7      ~6, ->16
          9    > > FE_FETCH_R                                               $7, !2, ->16
   40    10    >   INIT_USER_CALL                                2          'call_user_func', !2
         11        FETCH_OBJ_R                                      ~8      'object'
         12        SEND_USER                                                ~8
         13        SEND_USER                                                !1
         14        DO_FCALL                                      0          
   38    15      > JMP                                                      ->9
         16    >   FE_FREE                                                  $7
   42    17      > RETURN                                                   null

End of function invokeonleave

Function getproxiedmethod:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8I4Tb
function name:  getProxiedMethod
number of ops:  19
compiled vars:  !0 = $method, !1 = $methodReflection
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   46     1        NEW                                              $2      'ReflectionMethod'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_OBJ_FUNC_ARG                               $3      'object'
          4        SEND_FUNC_ARG                                            $3
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !1, $2
   47     8        INIT_METHOD_CALL                                         !1, 'getClosure'
          9        CHECK_FUNC_ARG                                           
         10        FETCH_OBJ_FUNC_ARG                               $6      'object'
         11        SEND_FUNC_ARG                                            $6
         12        DO_FCALL                                      0  $7      
         13        INIT_METHOD_CALL                                         $7, 'bindTo'
         14        FETCH_THIS                                       $8      
         15        SEND_VAR_EX                                              $8
         16        DO_FCALL                                      0  $9      
         17      > RETURN                                                   $9
   48    18*     > RETURN                                                   null

End of function getproxiedmethod

Function __call:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8I4Tb
function name:  __call
number of ops:  18
compiled vars:  !0 = $method, !1 = $arguments
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   52     2        INIT_METHOD_CALL                                         'invokeOnEnter'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0          
   53     6        INIT_METHOD_CALL                                         'getProxiedMethod'
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0  $3      
          9        INIT_USER_CALL                                0          'call_user_func_array', $3
         10        SEND_ARRAY                                               !1
         11        CHECK_UNDEF_ARGS                                         
         12        DO_FCALL                                      0          
   54    13        INIT_METHOD_CALL                                         'invokeOnLeave'
         14        SEND_VAR                                                 !0
         15        SEND_VAR                                                 !1
         16        DO_FCALL                                      0          
   55    17      > RETURN                                                   null

End of function __call

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

End of function __construct

End of class Proxy.

Class Foo:
Function bar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8I4Tb
function name:  bar
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   69     0  E >   INIT_METHOD_CALL                                         'qux'
          1        DO_FCALL                                      0          
   70     2      > RETURN                                                   null

End of function bar

Function qux:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8I4Tb
function name:  qux
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   INIT_METHOD_CALL                                         'zip'
          1        DO_FCALL                                      0          
   75     2      > RETURN                                                   null

End of function qux

Function zip:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8I4Tb
function name:  zip
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 'Foo'
          2        DO_ICALL                                                 
   80     3      > RETURN                                                   null

End of function zip

End of class Foo.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.75 ms | 1420 KiB | 17 Q