有没有办法找出PHP中的函数从何处调用? 例如:
function epic()
{
fail();
}
function fail()
{
//at this point, how do i know, that epic() has called this function?
}
答案 0 :(得分:122)
您可以使用debug_backtrace()
。
示例:强>
<?php
function epic( $a, $b )
{
fail( $a . ' ' . $b );
}
function fail( $string )
{
$backtrace = debug_backtrace();
print_r( $backtrace );
}
epic( 'Hello', 'World' );
<强>输出:强>
Array
(
[0] => Array
(
[file] => /Users/romac/Desktop/test.php
[line] => 5
[function] => fail
[args] => Array
(
[0] => Hello World
)
)
[1] => Array
(
[file] => /Users/romac/Desktop/test.php
[line] => 15
[function] => epic
[args] => Array
(
[0] => Hello
[1] => World
)
)
)
答案 1 :(得分:24)
function fail()
{
$backtrace = debug_backtrace();
// Here, $backtrace[0] points to fail(), so we'll look in $backtrace[1] instead
if (isset($backtrace[1]['function']) && $backtrace[1]['function'] == 'epic')
{
// Called by epic()...
}
}
答案 2 :(得分:16)
我找到的最快最简单的解决方案
usesOperand()
我在联想笔记本电脑上测试速度:英特尔Pentiom CPU N3530 2.16GHz,内存8GB
public function func() { //function whose call file you want to find
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
}
$trace: Array
(
[0] => Array
(
[file] => C:\wamp\www\index.php
[line] => 56
[function] => func
[class] => (func Class namespace)
[type] => ->
)
)
结果:
global $times;
$start = microtime(true);
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
$times[] = microtime(true) - $start;
答案 3 :(得分:14)
所以,如果你仍然真的不知道如何,那么解决方案就是:
$backtrace = debug_backtrace();
echo 'Mu name is '.$backtrace[1]['function'].', and I have called him! Muahahah!';
答案 4 :(得分:5)
使用debug_backtrace函数:http://php.net/manual/en/function.debug-backtrace.php
答案 5 :(得分:3)
尝试以下代码。
foreach(debug_backtrace() as $t) {
echo $t['file'] . ' line ' . $t['line'] . ' calls ' . $t['function'] . "()<br/>";
}
答案 6 :(得分:3)
如果要在堆栈顶部跟踪调用的确切来源,可以使用以下代码:
$call_origin = end(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
这将忽略链接函数并仅获取最相关的调用信息(相关内容松散使用,因为它取决于您要完成的操作)。
答案 7 :(得分:-1)
function findFunction($function, $inputDirectory=""){
//version 0.1
$docRoot = getenv("DOCUMENT_ROOT");
$folderArray = null;
$dirArray = null;
// open directory
$directory = opendir($docRoot.$inputDirectory);
// get each entry
while($entryName = readdir($directory)) {
if(is_dir($entryName) && $entryName != "." && $entryName != ".."){
$folderArray[] = str_replace($inputDirectory, "", $entryName);
}
$ext = explode(".", $entryName);
if(!empty($ext[1])){
$dirArray[] = $docRoot.$inputDirectory."/".$entryName;
}
}
// close directory
closedir($directory);
$found = false;
if(is_array($dirArray)){
foreach($dirArray as $current){
$myFile = file_get_contents($current);
$myFile = str_replace("<?php", "", $myFile);
$myFile = str_replace("?>", "", $myFile);
if(preg_match("/function ".$function."/", $myFile)){
$found = true;
$foundLocation = $current;
break;
}
}
}
if($found){
echo $foundLocation;
exit;
} else if(is_array($folderArray)){
foreach($folderArray as $folder){
if(!isset($return)){
$return = findFunction($function, $inputDirectory."/".$folder);
} else if($return == false){
$return = findFunction($function, $inputDirectory."/".$folder);
}
}
} else {
return false;
}
}
findFunction("testFunction", "rootDirectory");
希望它有所帮助。如果实际函数在httpdocs之外,则无法找到它,因为服务器将被设置为不允许它。只测试了一个文件夹,但递归方法应该在理论上有用。
这就像0.1版本,但我不打算继续开发它,所以如果有人更新它可以随意重新发布它。