PHP: Get PHP's variables, functions, constants from a php file

后端 未结 3 1712
暗喜
暗喜 2021-01-07 02:16

Is there a way to get user-defined php functions, variables, constants from a php file? Following functions are not the best way to do so because they get all decalred funct

相关标签:
3条回答
  • 2021-01-07 02:46

    call get_defined_functions, then include the file, call get_defined_functions once again and compute the difference.

    0 讨论(0)
  • 2021-01-07 02:59

    To get all variables defined in a file

    $init_var = get_defined_vars();
    include_once('file_to_check.php');
    $init_var2 = get_defined_vars();
    unset($init_var2['init_var']);
    $variables_defined_in_file = array_diff_key($init_var2, $init_var));
    var_dump($variables_defined_in_file);
    

    you should repeat above code by replacing get_defined_vars() for

    get_defined_vars()  
    get_defined_functions()  
    get_defined_constants()
    
    0 讨论(0)
  • 2021-01-07 03:09

    You probably want to try the PHP Tokenizer.

    http://www.php.net/manual/en/ref.tokenizer.php

    From an external script:

    <?php
    
    var_dump(token_get_all(file_get_contents('myscript.php')));
    
    ?>
    
    0 讨论(0)
提交回复
热议问题