3v4l.org

run code in 500+ PHP versions simultaneously
<?php if (! \function_exists('func_get_named_args')) { /** * Returns the arguments passed to the calling function, keyed by parameter name. * * Uses debug_backtrace and reflection to convert positional arguments * into an associative array keyed by the corresponding parameter names. * * See: https://gist.github.com/ghostwriter/82cffeb51b7052cba489f73a5350adf2 * * @return array<string, mixed> */ function func_get_named_args(): array { $caller = debug_backtrace(0, 2)[1]; $arguments = $caller['args'] ?? []; $reflectionMethod = isset($caller['class']) ? new ReflectionMethod($caller['class'], $caller['function']) : new ReflectionFunction($caller['function']); foreach ($reflectionMethod->getParameters() as $parameter) { $position = $parameter->getPosition(); if (isset($arguments[$position])) { $arguments[$parameter->getName()] = $arguments[$position]; unset($arguments[$position]); } } return $arguments; } } function callFunc(...$params) { $argv = func_get_named_args(); $argc = count($argv); print_r([ '$argc' => $argc, '$argv' => $argv, ]); } class User { public function register($email, $password, $active = null, $default = 0, ...$attributes) { $argv = func_get_named_args(); $argc = count($argv); print_r([ '$argc' => $argc, '$argv' => $argv, ]); return [$email, $password, $attributes]; } } $foo = new User(); $foo->register('admin@test.com', 'pass1', ...[ 'id' => 'uuid-1', 'phone' => '012-345-6789', 'role' => 'admin', ]); echo PHP_EOL . '--------' . PHP_EOL; callFunc(...[ 'id' => 'uuid-2', 'email' => 'test@test.com', 'phone' => '987-654-3210', 'name' => 'first last2', 'role' => 'tester', 'password' => 'pass2' ]);
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
filename:       /in/AquJa
function name:  (null)
number of ops:  21
compiled vars:  !0 = $foo
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                                   'function_exists'
          1        SEND_VAL                                                     'func_get_named_args'
          2        DO_ICALL                                             $1      
          3        BOOL_NOT                                             ~2      $1
          4      > JMPZ                                                         ~2, ->6
   14     5    >   DECLARE_FUNCTION                                             'func_get_named_args'
   61     6    >   NEW                                                  $3      'User'
          7        DO_FCALL                                          0          
          8        ASSIGN                                                       !0, $3
   63     9        INIT_METHOD_CALL                                             !0, 'register'
         10        SEND_VAL_EX                                                  'admin%40test.com'
         11        SEND_VAL_EX                                                  'pass1'
   64    12        SEND_UNPACK                                                  <array>
         13        CHECK_UNDEF_ARGS                                             
   63    14        DO_FCALL                                          1          
   69    15        ECHO                                                         '%0A--------%0A'
   71    16        INIT_FCALL                                                   'callfunc'
   72    17        SEND_UNPACK                                                  <array>
         18        CHECK_UNDEF_ARGS                                             
   71    19        DO_FCALL                                          1          
   78    20      > RETURN                                                       1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 22
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
2 jumps found. (Code = 77) Position 1 = 32, Position 2 = 45
Branch analysis from position: 32
2 jumps found. (Code = 78) Position 1 = 33, Position 2 = 45
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 44
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 44
Branch analysis from position: 45
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 45
Branch analysis from position: 22
2 jumps found. (Code = 77) Position 1 = 32, Position 2 = 45
Branch analysis from position: 32
Branch analysis from position: 45
filename:       /in/AquJa
function name:  func_get_named_args
number of ops:  50
compiled vars:  !0 = $caller, !1 = $arguments, !2 = $reflectionMethod, !3 = $parameter, !4 = $position
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   16     0  E >   INIT_FCALL                                                   'debug_backtrace'
          1        SEND_VAL                                                     0
          2        SEND_VAL                                                     2
          3        DO_ICALL                                             $5      
          4        FETCH_DIM_R                                          ~6      $5, 1
          5        ASSIGN                                                       !0, ~6
   17     6        FETCH_DIM_IS                                         ~8      !0, 'args'
          7        COALESCE                                             ~9      ~8
          8        QM_ASSIGN                                            ~9      <array>
          9        ASSIGN                                                       !1, ~9
   19    10        ISSET_ISEMPTY_DIM_OBJ                             0          !0, 'class'
         11      > JMPZ                                                         ~11, ->22
   20    12    >   NEW                                                  $12     'ReflectionMethod'
         13        CHECK_FUNC_ARG                                               
         14        FETCH_DIM_FUNC_ARG                                   $13     !0, 'class'
         15        SEND_FUNC_ARG                                                $13
         16        CHECK_FUNC_ARG                                               
         17        FETCH_DIM_FUNC_ARG                                   $14     !0, 'function'
         18        SEND_FUNC_ARG                                                $14
         19        DO_FCALL                                          0          
         20        QM_ASSIGN                                            ~16     $12
         21      > JMP                                                          ->28
   21    22    >   NEW                                                  $17     'ReflectionFunction'
         23        CHECK_FUNC_ARG                                               
         24        FETCH_DIM_FUNC_ARG                                   $18     !0, 'function'
         25        SEND_FUNC_ARG                                                $18
         26        DO_FCALL                                          0          
         27        QM_ASSIGN                                            ~16     $17
   19    28    >   ASSIGN                                                       !2, ~16
   23    29        INIT_METHOD_CALL                                             !2, 'getParameters'
         30        DO_FCALL                                          0  $21     
         31      > FE_RESET_R                                           $22     $21, ->45
         32    > > FE_FETCH_R                                                   $22, !3, ->45
   24    33    >   INIT_METHOD_CALL                                             !3, 'getPosition'
         34        DO_FCALL                                          0  $23     
         35        ASSIGN                                                       !4, $23
   25    36        ISSET_ISEMPTY_DIM_OBJ                             0          !1, !4
         37      > JMPZ                                                         ~25, ->44
   26    38    >   INIT_METHOD_CALL                                             !3, 'getName'
         39        DO_FCALL                                          0  $26     
         40        FETCH_DIM_R                                          ~28     !1, !4
         41        ASSIGN_DIM                                                   !1, $26
         42        OP_DATA                                                      ~28
   27    43        UNSET_DIM                                                    !1, !4
   23    44    > > JMP                                                          ->32
         45    >   FE_FREE                                                      $22
   31    46        VERIFY_RETURN_TYPE                                           !1
         47      > RETURN                                                       !1
   32    48*       VERIFY_RETURN_TYPE                                           
         49*     > RETURN                                                       null

End of Dynamic Function 0

Function callfunc:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AquJa
function name:  callFunc
number of ops:  12
compiled vars:  !0 = $params, !1 = $argv, !2 = $argc
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   35     0  E >   RECV_VARIADIC                                        !0      
   36     1        INIT_FCALL_BY_NAME                                           'func_get_named_args'
          2        DO_FCALL                                          0  $3      
          3        ASSIGN                                                       !1, $3
   37     4        COUNT                                                ~5      !1
          5        ASSIGN                                                       !2, ~5
   39     6        INIT_FCALL                                                   'print_r'
   40     7        INIT_ARRAY                                           ~7      !2, '%24argc'
   41     8        ADD_ARRAY_ELEMENT                                    ~7      !1, '%24argv'
          9        SEND_VAL                                                     ~7
   39    10        DO_ICALL                                                     
   43    11      > RETURN                                                       null

End of function callfunc

Class User:
Function register:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AquJa
function name:  register
number of ops:  20
compiled vars:  !0 = $email, !1 = $password, !2 = $active, !3 = $default, !4 = $attributes, !5 = $argv, !6 = $argc
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   47     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV_INIT                                            !2      null
          3        RECV_INIT                                            !3      0
          4        RECV_VARIADIC                                        !4      
   49     5        INIT_FCALL_BY_NAME                                           'func_get_named_args'
          6        DO_FCALL                                          0  $7      
          7        ASSIGN                                                       !5, $7
   50     8        COUNT                                                ~9      !5
          9        ASSIGN                                                       !6, ~9
   52    10        INIT_FCALL                                                   'print_r'
   53    11        INIT_ARRAY                                           ~11     !6, '%24argc'
   54    12        ADD_ARRAY_ELEMENT                                    ~11     !5, '%24argv'
         13        SEND_VAL                                                     ~11
   52    14        DO_ICALL                                                     
   57    15        INIT_ARRAY                                           ~13     !0
         16        ADD_ARRAY_ELEMENT                                    ~13     !1
         17        ADD_ARRAY_ELEMENT                                    ~13     !4
         18      > RETURN                                                       ~13
   58    19*     > RETURN                                                       null

End of function register

End of class User.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
147.91 ms | 847 KiB | 12 Q