3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CallableByName { private $closure; public function __construct(\Closure $closure) { $this->closure = $closure; } public function invoke(array $parameters) { return call_user_func($this->closure, $this->getOrderedFromNamed($parameters)); } public function getOrderedFromNamed(array $parameters) { $orderedParameters = []; $functionReflection = new \ReflectionFunction($this->getClosure()); foreach ($functionReflection->getParameters() as $parameterReflection) { /* @var $parameterReflection \ReflectionParameter */ $parameterName = $parameterReflection->getName(); if (isset($parameters[$parameterName])) { $orderedParameters[$parameterName] = $parameters[$parameterName]; continue; } if ($parameterReflection->isDefaultValueAvailable()) { $orderedParameters[$parameterName] = $parameterReflection->getDefaultValue(); continue; } $orderedParameters[$parameterName] = null; } return $orderedParameters; } } $callableByName = new CallableByName(function($foo, $bar, $qux = 'Holy Qux!', $zip = null) { var_dump(get_defined_vars()); }); $callableByName->invoke([ 'bar' => 'Hello', 'foo' => 'World', ]);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/i3uCo
function name:  (null)
number of ops:  9
compiled vars:  !0 = $callableByName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   NEW                                              $1      'CallableByName'
          1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fi3uCo%3A43%240'
   46     2        SEND_VAL_EX                                              ~2
          3        DO_FCALL                                      0          
   43     4        ASSIGN                                                   !0, $1
   48     5        INIT_METHOD_CALL                                         !0, 'invoke'
   49     6        SEND_VAL_EX                                              <array>
          7        DO_FCALL                                      0          
   51     8      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2Fi3uCo%3A43%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/i3uCo
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $foo, !1 = $bar, !2 = $qux, !3 = $zip
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      'Holy+Qux%21'
          3        RECV_INIT                                        !3      null
   45     4        INIT_FCALL                                               'var_dump'
          5        INIT_FCALL                                               'get_defined_vars'
          6        DO_ICALL                                         $4      
          7        SEND_VAR                                                 $4
          8        DO_ICALL                                                 
   46     9      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fi3uCo%3A43%240

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

End of function __construct

Function invoke:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/i3uCo
function name:  invoke
number of ops:  10
compiled vars:  !0 = $parameters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   15     1        FETCH_OBJ_R                                      ~1      'closure'
          2        INIT_USER_CALL                                1          'call_user_func', ~1
          3        INIT_METHOD_CALL                                         'getOrderedFromNamed'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0  $2      
          6        SEND_USER                                                $2
          7        DO_FCALL                                      0  $3      
          8      > RETURN                                                   $3
   16     9*     > RETURN                                                   null

End of function invoke

Function getorderedfromnamed:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 32
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 32
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 21
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 29
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
filename:       /in/i3uCo
function name:  getOrderedFromNamed
number of ops:  35
compiled vars:  !0 = $parameters, !1 = $orderedParameters, !2 = $functionReflection, !3 = $parameterReflection, !4 = $parameterName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
   20     1        ASSIGN                                                   !1, <array>
   21     2        NEW                                              $6      'ReflectionFunction'
          3        INIT_METHOD_CALL                                         'getClosure'
          4        DO_FCALL                                      0  $7      
          5        SEND_VAR_NO_REF_EX                                       $7
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !2, $6
   22     8        INIT_METHOD_CALL                                         !2, 'getParameters'
          9        DO_FCALL                                      0  $10     
         10      > FE_RESET_R                                       $11     $10, ->32
         11    > > FE_FETCH_R                                               $11, !3, ->32
   25    12    >   INIT_METHOD_CALL                                         !3, 'getName'
         13        DO_FCALL                                      0  $12     
         14        ASSIGN                                                   !4, $12
   26    15        ISSET_ISEMPTY_DIM_OBJ                         0          !0, !4
         16      > JMPZ                                                     ~14, ->21
   28    17    >   FETCH_DIM_R                                      ~16     !0, !4
         18        ASSIGN_DIM                                               !1, !4
         19        OP_DATA                                                  ~16
   29    20      > JMP                                                      ->11
   31    21    >   INIT_METHOD_CALL                                         !3, 'isDefaultValueAvailable'
         22        DO_FCALL                                      0  $17     
         23      > JMPZ                                                     $17, ->29
   33    24    >   INIT_METHOD_CALL                                         !3, 'getDefaultValue'
         25        DO_FCALL                                      0  $19     
         26        ASSIGN_DIM                                               !1, !4
         27        OP_DATA                                                  $19
   34    28      > JMP                                                      ->11
   36    29    >   ASSIGN_DIM                                               !1, !4
         30        OP_DATA                                                  null
   22    31      > JMP                                                      ->11
         32    >   FE_FREE                                                  $11
   38    33      > RETURN                                                   !1
   39    34*     > RETURN                                                   null

End of function getorderedfromnamed

End of class CallableByName.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.82 ms | 1404 KiB | 17 Q