Tow*_*wer 25 php debugging function
如何找出调用我的函数的脚本的文件名?
例如,
function sthing() {
echo __FILE__; // echoes myself
echo __CALLER_FILE__; // echoes the file that called me
}
Run Code Online (Sandbox Code Playgroud)
Pas*_*TIN 35
解决方案可能是使用debug_backtrace函数:在回溯中,应该存在这种信息.
或者,正如戈登在评论中指出的那样,debug_print_backtrace如果您只想输出该信息而不使用它,您也可以使用它.
例如,temp.php包含这个:
<?php
include 'temp-2.php';
my_function();
Run Code Online (Sandbox Code Playgroud)
并与temp-2.php含有这样的:
<?php
function my_function() {
var_dump(debug_backtrace());
}
Run Code Online (Sandbox Code Playgroud)
temp.php (i.e. the first script)从我的浏览器
调用获取此输出:
array
0 =>
array
'file' => string '/.../temp/temp.php' (length=46)
'line' => int 5
'function' => string 'my_function' (length=11)
'args' =>
array
empty
Run Code Online (Sandbox Code Playgroud)
在那里,我有" temp.php"文件名 - 这是调用该函数的文件名.
当然,你需要多测试一下(特别是在函数不在"第一级"包含文件的情况下,但在另一个文件中包含的文件中 - 不确定debug_backtrace会有多大帮助,那里...... ) ; 但这可能会帮助你获得第一个想法......
试试这个代码:
$key = array_search(__FUNCTION__, array_column(debug_backtrace(), 'function'));
var_dump(debug_backtrace()[$key]['file']);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18506 次 |
| 最近记录: |