3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Container { protected $target; protected $className; protected $methods = array(); public function __construct( $target ) { $this->target = $target; } public function attach( $name, $method ) { if ( !$this->className ) { $this->className = get_class( $this->target ); } $binded = Closure::bind( $method, $this->target, $this->className ); $this->methods[$name] = $binded; } public function __call( $name, $arguments ) { if ( array_key_exists( $name, $this->methods ) ) { return call_user_func_array( $this->methods[$name] , $arguments ); } if ( method_exists( $this->target, $name ) ) { return call_user_func_array( array( $this->target, $name ), $arguments ); } } } class Foo { private $bar = 'payload'; }; $foobar = new Foo; // you initial object $instance = new Container( $foobar ); $func = function ( $param ) { return 'Get ' . $this->bar . ' and ' . $param; }; $instance->attach('test', $func); // setting up the whole thing echo $instance->test('lorem ipsum'); // 'Get payload and lorem ipsum'
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/naiMY
function name:  (null)
number of ops:  18
compiled vars:  !0 = $foobar, !1 = $instance, !2 = $func
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   NEW                                              $3      'Foo'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $3
   49     3        NEW                                              $6      'Container'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !1, $6
   50     7        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FnaiMY%3A50%240'
          8        ASSIGN                                                   !2, ~9
   54     9        INIT_METHOD_CALL                                         !1, 'attach'
         10        SEND_VAL_EX                                              'test'
         11        SEND_VAR_EX                                              !2
         12        DO_FCALL                                      0          
   58    13        INIT_METHOD_CALL                                         !1, 'test'
         14        SEND_VAL_EX                                              'lorem+ipsum'
         15        DO_FCALL                                      0  $12     
         16        ECHO                                                     $12
   59    17      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FnaiMY%3A50%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/naiMY
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $param
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
   52     1        FETCH_THIS                                       $1      
          2        FETCH_OBJ_R                                      ~2      $1, 'bar'
          3        CONCAT                                           ~3      'Get+', ~2
          4        CONCAT                                           ~4      ~3, '+and+'
          5        CONCAT                                           ~5      ~4, !0
          6      > RETURN                                                   ~5
   53     7*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FnaiMY%3A50%240

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

End of function __construct

Function attach:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/naiMY
function name:  attach
number of ops:  21
compiled vars:  !0 = $name, !1 = $method, !2 = $binded
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   16     2        FETCH_OBJ_R                                      ~3      'className'
          3        BOOL_NOT                                         ~4      ~3
          4      > JMPZ                                                     ~4, ->9
   18     5    >   FETCH_OBJ_R                                      ~6      'target'
          6        GET_CLASS                                        ~7      ~6
          7        ASSIGN_OBJ                                               'className'
          8        OP_DATA                                                  ~7
   20     9    >   INIT_STATIC_METHOD_CALL                                  'Closure', 'bind'
         10        SEND_VAR                                                 !1
         11        FETCH_OBJ_R                                      ~8      'target'
         12        SEND_VAL                                                 ~8
         13        FETCH_OBJ_R                                      ~9      'className'
         14        SEND_VAL                                                 ~9
         15        DO_FCALL                                      0  $10     
         16        ASSIGN                                                   !2, $10
   21    17        FETCH_OBJ_W                                      $12     'methods'
         18        ASSIGN_DIM                                               $12, !0
         19        OP_DATA                                                  !2
   22    20      > RETURN                                                   null

End of function attach

Function __call:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 26
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/naiMY
function name:  __call
number of ops:  27
compiled vars:  !0 = $name, !1 = $arguments
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   26     2        FETCH_OBJ_R                                      ~2      'methods'
          3        ARRAY_KEY_EXISTS                                         !0, ~2
          4      > JMPZ                                                     ~3, ->12
   28     5    >   FETCH_OBJ_R                                      ~4      'methods'
          6        FETCH_DIM_R                                      ~5      ~4, !0
          7        INIT_USER_CALL                                0          'call_user_func_array', ~5
          8        SEND_ARRAY                                               !1
          9        CHECK_UNDEF_ARGS                                         
         10        DO_FCALL                                      0  $6      
         11      > RETURN                                                   $6
   31    12    >   INIT_FCALL                                               'method_exists'
         13        FETCH_OBJ_R                                      ~7      'target'
         14        SEND_VAL                                                 ~7
         15        SEND_VAR                                                 !0
         16        DO_ICALL                                         $8      
         17      > JMPZ                                                     $8, ->26
   34    18    >   FETCH_OBJ_R                                      ~9      'target'
         19        INIT_ARRAY                                       ~10     ~9
         20        ADD_ARRAY_ELEMENT                                ~10     !0
         21        INIT_USER_CALL                                0          'call_user_func_array', ~10
   35    22        SEND_ARRAY                                               !1
         23        CHECK_UNDEF_ARGS                                         
         24        DO_FCALL                                      0  $11     
         25      > RETURN                                                   $11
   38    26    > > RETURN                                                   null

End of function __call

End of class Container.

Class Foo: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.63 ms | 1404 KiB | 15 Q