3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * composer require nikic/php-parser */ require __DIR__ . '/vendor/autoload.php'; use PhpParser\ParserFactory; /** * 获取调用函数的参数 * * @param array $backtrace debug_backtrace * @param string $func 要查找的函数名字 * * @return array 返回调用的参数,如果是变量,就返回变量名,如果不是变量是值,就返回序列号,从参数索引 0 开始编号 */ function getCallFuncArgs(array $backtrace, string $func): array { // 获取调用栈中第一个 $caller = array_shift($backtrace); // 取出文件 $file = $caller['file']; // 创建解析器 $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); $code = file_get_contents($file); // 解析代码 $ast = $parser->parse($code); $nodeFinder = new \PhpParser\NodeFinder(); // 查找所有的函数调用 $nodes = $nodeFinder->findInstanceOf($ast, \PhpParser\Node\Expr\FuncCall::class); $argList = []; foreach ($nodes as $node) { // 找到函数名跟要查找的一致的 if (($node->name->parts[0] ?? null) === $func) { // 创建索引值 $i = 0; // 遍历所有参数 foreach ($node->args as $arg) { // 如果是变量,就取变量名 if ($arg->value instanceof \PhpParser\Node\Expr\Variable) { $argList[] = $arg->value->name; } else { // 否则就编号 $argList[] = '#' . $i; } $i++; } } } return $argList; } function testA() { // 调用 $keys = getCallFuncArgs(debug_backtrace(), __FUNCTION__); // 填充一个关联数组,创建关系 $combine = array_combine($keys, func_get_args()); var_dump($combine); } $a = 1; $b = 2; $c = 2; testA($a, $b, 1, 2, 3, 4, $c);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/elVlX
function name:  (null)
number of ops:  14
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   INCLUDE_OR_EVAL                                          '%2Fin%2Fvendor%2Fautoload.php', REQUIRE
   65     1        ASSIGN                                                   !0, 1
   66     2        ASSIGN                                                   !1, 2
   67     3        ASSIGN                                                   !2, 2
   68     4        INIT_FCALL                                               'testa'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        SEND_VAL                                                 1
          8        SEND_VAL                                                 2
          9        SEND_VAL                                                 3
         10        SEND_VAL                                                 4
         11        SEND_VAR                                                 !2
         12        DO_FCALL                                      0          
         13      > RETURN                                                   1

Function getcallfuncargs:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 33, Position 2 = 60
Branch analysis from position: 33
2 jumps found. (Code = 78) Position 1 = 34, Position 2 = 60
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 59
Branch analysis from position: 41
2 jumps found. (Code = 77) Position 1 = 44, Position 2 = 58
Branch analysis from position: 44
2 jumps found. (Code = 78) Position 1 = 45, Position 2 = 58
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 48, Position 2 = 53
Branch analysis from position: 48
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
Branch analysis from position: 53
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
Branch analysis from position: 58
Branch analysis from position: 59
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 60
filename:       /in/elVlX
function name:  getCallFuncArgs
number of ops:  65
compiled vars:  !0 = $backtrace, !1 = $func, !2 = $caller, !3 = $file, !4 = $parser, !5 = $code, !6 = $ast, !7 = $nodeFinder, !8 = $nodes, !9 = $argList, !10 = $node, !11 = $i, !12 = $arg
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   22     2        INIT_FCALL                                               'array_shift'
          3        SEND_REF                                                 !0
          4        DO_ICALL                                         $13     
          5        ASSIGN                                                   !2, $13
   24     6        FETCH_DIM_R                                      ~15     !2, 'file'
          7        ASSIGN                                                   !3, ~15
   26     8        NEW                                              $17     'PhpParser%5CParserFactory'
          9        DO_FCALL                                      0          
         10        INIT_METHOD_CALL                                         $17, 'create'
         11        FETCH_CLASS_CONSTANT                             ~19     'PhpParser%5CParserFactory', 'PREFER_PHP7'
         12        SEND_VAL_EX                                              ~19
         13        DO_FCALL                                      0  $20     
         14        ASSIGN                                                   !4, $20
   27    15        INIT_FCALL                                               'file_get_contents'
         16        SEND_VAR                                                 !3
         17        DO_ICALL                                         $22     
         18        ASSIGN                                                   !5, $22
   29    19        INIT_METHOD_CALL                                         !4, 'parse'
         20        SEND_VAR_EX                                              !5
         21        DO_FCALL                                      0  $24     
         22        ASSIGN                                                   !6, $24
   30    23        NEW                                              $26     'PhpParser%5CNodeFinder'
         24        DO_FCALL                                      0          
         25        ASSIGN                                                   !7, $26
   32    26        INIT_METHOD_CALL                                         !7, 'findInstanceOf'
         27        SEND_VAR_EX                                              !6
         28        SEND_VAL_EX                                              'PhpParser%5CNode%5CExpr%5CFuncCall'
         29        DO_FCALL                                      0  $29     
         30        ASSIGN                                                   !8, $29
   33    31        ASSIGN                                                   !9, <array>
   34    32      > FE_RESET_R                                       $32     !8, ->60
         33    > > FE_FETCH_R                                               $32, !10, ->60
   36    34    >   FETCH_OBJ_IS                                     ~33     !10, 'name'
         35        FETCH_OBJ_IS                                     ~34     ~33, 'parts'
         36        FETCH_DIM_IS                                     ~35     ~34, 0
         37        COALESCE                                         ~36     ~35
         38        QM_ASSIGN                                        ~36     null
         39        IS_IDENTICAL                                             !1, ~36
         40      > JMPZ                                                     ~37, ->59
   38    41    >   ASSIGN                                                   !11, 0
   40    42        FETCH_OBJ_R                                      ~39     !10, 'args'
         43      > FE_RESET_R                                       $40     ~39, ->58
         44    > > FE_FETCH_R                                               $40, !12, ->58
   42    45    >   FETCH_OBJ_R                                      ~41     !12, 'value'
         46        INSTANCEOF                                               ~41, 'PhpParser%5CNode%5CExpr%5CVariable'
         47      > JMPZ                                                     ~42, ->53
   43    48    >   FETCH_OBJ_R                                      ~44     !12, 'value'
         49        FETCH_OBJ_R                                      ~45     ~44, 'name'
         50        ASSIGN_DIM                                               !9
         51        OP_DATA                                                  ~45
         52      > JMP                                                      ->56
   46    53    >   CONCAT                                           ~47     '%23', !11
         54        ASSIGN_DIM                                               !9
         55        OP_DATA                                                  ~47
   49    56    >   PRE_INC                                                  !11
   40    57      > JMP                                                      ->44
         58    >   FE_FREE                                                  $40
   34    59    > > JMP                                                      ->33
         60    >   FE_FREE                                                  $32
   53    61        VERIFY_RETURN_TYPE                                       !9
         62      > RETURN                                                   !9
   54    63*       VERIFY_RETURN_TYPE                                       
         64*     > RETURN                                                   null

End of function getcallfuncargs

Function testa:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/elVlX
function name:  testA
number of ops:  17
compiled vars:  !0 = $keys, !1 = $combine
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   INIT_FCALL                                               'getcallfuncargs'
          1        INIT_FCALL                                               'debug_backtrace'
          2        DO_ICALL                                         $2      
          3        SEND_VAR                                                 $2
          4        SEND_VAL                                                 'testA'
          5        DO_FCALL                                      0  $3      
          6        ASSIGN                                                   !0, $3
   61     7        INIT_FCALL                                               'array_combine'
          8        SEND_VAR                                                 !0
          9        FUNC_GET_ARGS                                    ~5      
         10        SEND_VAL                                                 ~5
         11        DO_ICALL                                         $6      
         12        ASSIGN                                                   !1, $6
   62    13        INIT_FCALL                                               'var_dump'
         14        SEND_VAR                                                 !1
         15        DO_ICALL                                                 
   63    16      > RETURN                                                   null

End of function testa

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.13 ms | 1411 KiB | 25 Q