gpt4 book ai didi

php - 找出在 PHP 中调用我的函数的文件名

转载 作者:IT王子 更新时间:2023-10-28 23:45:25 26 4
gpt4 key购买 nike

如何找出调用我的函数的脚本的文件名?

例如,

function sthing() {
echo __FILE__; // echoes myself
echo __CALLER_FILE__; // echoes the file that called me
}

最佳答案

解决方案可能是使用 debug_backtrace function : 在回溯中,应该存在这种信息。

或者,正如 Gordon 在评论中指出的那样,您也可以使用 debug_print_backtrace如果您只想输出该信息而不使用它。


例如,temp.php 包含这个:

<?php
include 'temp-2.php';
my_function();

temp-2.php 包含这个:

<?php
function my_function() {
var_dump(debug_backtrace());
}


从我的浏览器调用 temp.php (即第一个脚本) 会得到这个输出:

array
0 =>
array
'file' => string '/.../temp/temp.php' (length=46)
'line' => int 5
'function' => string 'my_function' (length=11)
'args' =>
array
empty

在那里,我有“temp.php”文件名——这是调用函数的文件名。


当然,您将不得不测试更多(尤其是在函数不在“第一级”包含文件中,而是在另一个文件包含的文件中的情况下 - 不确定 debug_backtrace 会有很大帮助,那里...) ;但这可能会帮助您获得第一个想法...

关于php - 找出在 PHP 中调用我的函数的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1935867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com