我知道函数debug_backtrace,但我正在寻找一些准备使用函数的实现,如GetCallingMethodName()?如果它也给出了方法的类(如果它确实是一个方法),那就太完美了。
当前回答
debug_backtrace()函数是知道这一点的唯一方法,如果你很懒,这是你应该自己编写GetCallingMethodName()的另一个原因。战胜懒惰!: D
其他回答
对我来说,debug_backtrace达到了内存限制,我想在生产中使用它来记录和发送发生的错误。
相反,我发现这个解决方案非常有效!
// Make a new exception at the point you want to trace, and trace it!
$e = new Exception;
var_dump($e->getTraceAsString());
// Outputs the following
#2 /usr/share/php/PHPUnit/Framework/TestCase.php(626): SeriesHelperTest->setUp()
#3 /usr/share/php/PHPUnit/Framework/TestResult.php(666): PHPUnit_Framework_TestCase->runBare()
#4 /usr/share/php/PHPUnit/Framework/TestCase.php(576): PHPUnit_Framework_TestResult->run(Object(SeriesHelperTest))
#5 /usr/share/php/PHPUnit/Framework/TestSuite.php(757): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#6 /usr/share/php/PHPUnit/Framework/TestSuite.php(733): PHPUnit_Framework_TestSuite->runTest(Object(SeriesHelperTest), Object(PHPUnit_Framework_TestResult))
#7 /usr/share/php/PHPUnit/TextUI/TestRunner.php(305): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult), false, Array, Array, false)
#8 /usr/share/php/PHPUnit/TextUI/Command.php(188): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#9 /usr/share/php/PHPUnit/TextUI/Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#10 /usr/bin/phpunit(53): PHPUnit_TextUI_Command::main()
#11 {main}"
debug_backtrace()函数是知道这一点的唯一方法,如果你很懒,这是你应该自己编写GetCallingMethodName()的另一个原因。战胜懒惰!: D
我最喜欢的方式,一句话!
debug_backtrace()[1]['function'];
你可以这样使用它:
echo 'The calling function: ' . debug_backtrace()[1]['function'];
注意,这只与去年发布的PHP版本兼容。但出于安全考虑,让PHP保持最新是一个好主意。
我刚刚写了一个名为“get_caller”的版本,我希望它能有所帮助。我的爸爸很懒。你可以从函数中运行get_caller(),你不需要像这样指定它:
get_caller(__FUNCTION__);
下面是完整的脚本和一个古怪的测试用例:
<?php
/* This function will return the name string of the function that called $function. To return the
caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
*/
function get_caller($function = NULL, $use_stack = NULL) {
if ( is_array($use_stack) ) {
// If a function stack has been provided, used that.
$stack = $use_stack;
} else {
// Otherwise create a fresh one.
$stack = debug_backtrace();
echo "\nPrintout of Function Stack: \n\n";
print_r($stack);
echo "\n";
}
if ($function == NULL) {
// We need $function to be a function name to retrieve its caller. If it is omitted, then
// we need to first find what function called get_caller(), and substitute that as the
// default $function. Remember that invoking get_caller() recursively will add another
// instance of it to the function stack, so tell get_caller() to use the current stack.
$function = get_caller(__FUNCTION__, $stack);
}
if ( is_string($function) && $function != "" ) {
// If we are given a function name as a string, go through the function stack and find
// it's caller.
for ($i = 0; $i < count($stack); $i++) {
$curr_function = $stack[$i];
// Make sure that a caller exists, a function being called within the main script
// won't have a caller.
if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
return $stack[$i + 1]["function"];
}
}
}
// At this stage, no caller has been found, bummer.
return "";
}
// TEST CASE
function woman() {
$caller = get_caller(); // No need for get_caller(__FUNCTION__) here
if ($caller != "") {
echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
} else {
echo "no-one called ", __FUNCTION__, "()\n";
}
}
function man() {
// Call the woman.
woman();
}
// Don't keep him waiting
man();
// Try this to see what happens when there is no caller (function called from main script)
//woman();
?>
Man()调用woman(),后者调用get_caller()。Get_caller()还不知道是谁调用了它,因为woman()很谨慎,没有告诉它,所以它递归查找。然后返回谁调用了woman()。在浏览器中以源代码模式的打印输出显示了函数堆栈:
Printout of Function Stack:
Array
(
[0] => Array
(
[file] => /Users/Aram/Development/Web/php/examples/get_caller.php
[line] => 46
[function] => get_caller
[args] => Array
(
)
)
[1] => Array
(
[file] => /Users/Aram/Development/Web/php/examples/get_caller.php
[line] => 56
[function] => woman
[args] => Array
(
)
)
[2] => Array
(
[file] => /Users/Aram/Development/Web/php/examples/get_caller.php
[line] => 60
[function] => man
[args] => Array
(
)
)
)
man() called woman(). No surprises there.
你也可以使用php异常提供的信息,这是一个优雅的解决方案:
function GetCallingMethodName(){ $e = new Exception(); $trace = $e->getTrace(); //position 0 would be the line that called this function so we ignore it $last_call = $trace[1]; print_r($last_call); } function firstCall($a, $b){ theCall($a, $b); } function theCall($a, $b){ GetCallingMethodName(); } firstCall('lucia', 'php');
你会得到这个…(瞧!)
Array ( [file] => /home/lufigueroa/Desktop/test.php [line] => 12 [function] => theCall [args] => Array ( [0] => lucia [1] => php ) )
推荐文章
- 编写器更新和安装之间有什么区别?
- 为什么Android工作室说“等待调试器”如果我不调试?
- 本地机器上的PHP服务器?
- 如何评论laravel .env文件?
- 在PHP中检测移动设备的最简单方法
- 如何在树枝模板中呈现DateTime对象
- 如何删除查询字符串,只得到URL?
- 您是否可以“编译”PHP代码并上传一个二进制文件,该文件将由字节码解释器运行?
- 非法字符串偏移警告PHP
- 从数组中获取随机项
- 没有找到用于调试模式的此可执行文件的有效配置文件
- 为什么一个函数检查字符串是否为空总是返回true?
- Visual Studio c++和Windows中的调试内存填充模式是什么?
- 如何使用Laravel迁移将时间戳列的默认值设置为当前时间戳?
- 如何增加php的最大执行时间