我知道debug_backtrace
函数,但我正在寻找一些可以直接使用的类似于GetCallingMethodName()
函数的实现?如果这个函数能够提供方法所在的类(如果确实是一个方法),那就更好了。
我知道debug_backtrace
函数,但我正在寻找一些可以直接使用的类似于GetCallingMethodName()
函数的实现?如果这个函数能够提供方法所在的类(如果确实是一个方法),那就更好了。
最简单的方法是:
echo debug_backtrace()[1]['function'];
如下评论所述,这可以通过传递参数来进一步优化:
object
和args
索引echo debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT|DEBUG_BACKTRACE_IGNORE_ARGS,2)[1]['function'];
print_r(debug_backtrace())
,但它返回的信息太多了,导致我的浏览器崩溃了。 - Mityadebug_backtrace()
时崩溃,那么可能存在其他更严重的问题。这可能是因为您将大型对象作为参数传递,或者您的调用堆栈非常深,或者您正在使用相当不稳定的浏览器! - Bobby Jackdebug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,2)[1]['function']
。 - Eugen Mihailescudebug_backtrace()
函数是唯一的途径来获取这个信息,如果你很懒,那么这就是另外一个理由让你自己编写GetCallingMethodName()
函数。 打败懒惰! :D
从 PHP 5.4 版本开始,你可以使用
$dbt=debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,2);
$caller = isset($dbt[1]['function']) ? $dbt[1]['function'] : null;
这样做不会浪费内存,因为它忽略参数并仅返回最后2个回溯堆栈条目,并且不会生成提示信息,就像其他答案一样。
echo $caller;
应该可以解决问题。;-) - Minister$caller = $dbt[1]['function'] ?? null;
。 - mbomb007isset($something) ? $something : null
缩短为 $something ?? null
。编辑:很抱歉,这已经有人提到了。 - thomasrutterdebug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,2)[1]['function'] ?? null;
- But those new buttons though..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); }并且您将得到以下结果...(voilà!)
function firstCall($a, $b){ theCall($a, $b); }
function theCall($a, $b){ GetCallingMethodName(); }
firstCall('lucia', 'php');
数组 ( [file] => /home/lufigueroa/Desktop/test.php [line] => 12 [function] => theCall [args] => 数组 ( [0] => lucia [1] => php )
)
对我来说,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()[1]['function'];
您可以像这样使用它:
echo 'The calling function: ' . debug_backtrace()[1]['function'];
注意,这仅与在过去一年内发布的PHP版本兼容。但出于安全原因,保持PHP更新始终是一个好主意。
注意,这仅与在过去一年内发布的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(),woman()调用get_caller()。由于woman()非常谨慎并没有告诉get_caller()是谁调用了它,所以get_caller()会递归查找调用者。然后它返回调用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.
我需要一个可以列出调用类/方法的东西(正在处理一个Magento项目)。
虽然debug_backtrace
提供了大量有用的信息,但它为Magento安装产生的信息量太多了(超过82,000行!)。因为我只关心调用函数和类,所以我想出了这个小解决方案:
$callers = debug_backtrace();
foreach( $callers as $call ) {
echo "<br>" . $call['class'] . '->' . $call['function'];
}
$caller = next(debug_backtrace())['function'];
PHP Notice: Only variables should be passed by reference
- 硬编码数组索引感觉很俗气,但我猜如果你想要一行代码,那就只能这样做了。 - Full Stack Alien我看到的最佳答案是:
list(, $caller) = debug_backtrace(false);
debug_backtrace()[1]['function']
这个不吸引你吗? - AgileTillIDielist(, $caller)
是完全有效的 PHP 代码;手册中指出:"list() 构造函数不再能够为空
." 这个构造函数并不是空的。关于这个来自2012年的回答,有什么不正确的地方吗?从 PHP 5.3.6 开始,该方法的签名为 debug_backtrace(int $options)
,不接受 false
参数。 - bryonbean