当我们php工作量变大之后,经常需要判断我们是否定义过某个变量,我们是否定义过某个常量,或者说我们是否定义过某个函数等等,我们重复定义常量会导致错误,我们重复定义变量会导致前面的值被覆盖,因此,还是很有必要去了解这些函数的。
比如说我在我的starphp框架里写一个文件,代码如下:
<?phpecho "当前用户定义的常量";$user_constants = get_defined_constants(TRUE);print_r($user_constants['user']);echo "<br />";echo "当前引入的文件";$files = get_included_files();print_r($files);
那么输出的结果为:
当前用户定义的常量Array ( [ROOT] => D:\MyApp\wamp\www\starshop [D] => \ [STAR] => D:\MyApp\wamp\www\starshop\star [CORE] => D:\MyApp\wamp\www\starshop\star\core [HOST] => localhost [APP] => D:\MyApp\wamp\www\starshop\app [LOG] => D:\MyApp\wamp\www\starshop\app\data\log [MODULE] => D:\MyApp\wamp\www\starshop\app\index [VIEW] => D:\MyApp\wamp\www\starshop\app\index\view ) 当前引入的文件Array ( [0] => D:\MyApp\wamp\www\starshop\index.php [1] => D:\MyApp\wamp\www\starshop\star\star.php [2] => D:\MyApp\wamp\www\starshop\star\core\config.php [3] => D:\MyApp\wamp\www\starshop\star\core\fun.php [4] => D:\MyApp\wamp\www\starshop\star\core\core.php [5] => D:\MyApp\wamp\www\starshop\star\core\control.php [6] => D:\MyApp\wamp\www\starshop\app\index\control\index.c.php [7] => D:\MyApp\wamp\www\starshop\app\index\view\index.php )
下面是这些函数的说明:
get_defined_vars:获取用户定义的变量get_defined_functions:获取所有已定义的函数get_loaded_extensions:获取所有可用的模块get_extension_funcs($module_name):获取指定模块的可用函数get_defined_constants():获取所有常量get_declared_classes():获取已定义的类get_included_files():获取导入的文件