Function list of php file

前端 未结 12 1284
南方客
南方客 2020-11-28 14:40

How to get list of functions that are declared in a php file

12条回答
  •  -上瘾入骨i
    2020-11-28 14:59

    To retrieve also the params definition: (Source in comments)

    Array

    (
        [0] => function foo ( &&bar, &big, [$small = 1] )
    
        [1] => function bar ( &foo )
    
        [2] => function noparams (  )
    
        [3] => function byrefandopt ( [&$the = one] )
    
    )
    

    $functions = get_defined_functions();
    $functions_list = array();
    foreach ($functions['user'] as $func) {
            $f = new ReflectionFunction($func);
            $args = array();
            foreach ($f->getParameters() as $param) {
                    $tmparg = '';
                    if ($param->isPassedByReference()) $tmparg = '&';
                    if ($param->isOptional()) {
                            $tmparg = '[' . $tmparg . '$' . $param->getName() . ' = ' . $param->getDefaultValue() . ']';
                    } else {
                            $tmparg.= '&' . $param->getName();
                    }
                    $args[] = $tmparg;
                    unset ($tmparg);
            }
            $functions_list[] = 'function ' . $func . ' ( ' . implode(', ', $args) . ' )' . PHP_EOL;
    }
    print_r($functions_list);
    

提交回复
热议问题