PHP:从一个php文件中获取PHP的变量、函数、常量 [英] PHP: Get PHP's variables, functions, constants from a php file

查看:50
本文介绍了PHP:从一个php文件中获取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')));

?>

这篇关于PHP:从一个php文件中获取PHP的变量、函数、常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆