PHP:从一个php文件中获取PHP的变量、函数、常量

2022-01-25 00:00:00 function file variables include php

有没有办法从 php 文件中获取用户定义的 php 函数、变量、常量?以下功能不是最好的方法,因为它们都被贴花functions/vars/constants(带有数百个 php 的内置常量和内部 php 函数):

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 functions/vars/constants (with hundreds of php's built-in constants and internal php functions):

get_defined_vars  
get_defined_functions  
get_defined_constants  

假设我有这个文件myfile.php:

<?php
$title = 'Sample Application';
$copyright = 'Copyright &copy; 2009';
    $my_array = array('sarfraz', 'ahmed', 'chandio');

define ('_CASTE', 'chandio');
define ('_COUNTRY', 'Pakistan');


function add($val1, $val2)
{
    return ($val1 + $val2);
}

function subtract($val1, $val2)
{
    return ($val1 - $val2);
}
?>

现在我如何从该文件中获取所有变量/函数/常量并可能存储在一个数组中?

Now how can i get all variables/functions/constants from that file and probably store in an array?

谢谢

推荐答案

你可能想试试 PHP Tokenizer.

You probably want to try the PHP Tokenizer.

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

来自外部脚本:

<?php

var_dump(token_get_all(file_get_contents('myscript.php')));

?>

相关文章