我知道函数debug_backtrace
,但我正在寻找一些随时可以使用的函数实现,如GetCallingMethodName()
?如果它也给出了方法的类,那就更好了(如果它确实是一个方法)。
发布于 2012-06-28 12:24:58
最简单的方法是:
echo debug_backtrace()[1]['function'];
发布于 2015-02-19 17:02:21
从php 5.4开始,您可以使用
$dbt=debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,2);
$caller = isset($dbt[1]['function']) ? $dbt[1]['function'] : null;
这不会浪费内存,因为它忽略了参数,只返回最后两个回溯堆栈条目,并且不会像这里的其他答案那样生成通知。
发布于 2014-02-27 18:53:15
对我来说,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}"
https://stackoverflow.com/questions/2110732
复制相似问题