如何在PHP中获取调用函数/方法的名称?
我知道函数debug_backtrace
,但我正在寻找一些准备使用像GetCallingMethodName()
函数的实现? 如果它也给方法的类也是完美的(如果它的确是一种方法的话)。
debug_backtrace()
函数是知道这一点的唯一方法,如果你懒惰,那么你应该自己编写GetCallingMethodName()
的另一个原因。 战斗懒惰! :d
最简单的方法是:
echo debug_backtrace()[1]['function'];
您也可以使用phpexception提供的信息,这是一个优雅的解决scheme:
函数GetCallingMethodName(){ $ e = new Exception(); $ trace = $ e-> getTrace(); //位置0将是调用这个函数的行,所以我们忽略它 $ last_call = $ trace [1]; 的print_r($ last_call); } 函数firstCall($ a,$ b){ theCall($ a,$ b); } 函数theCall($ a,$ b){ GetCallingMethodName(); } firstCall('lucia','php');
你得到这个…(瞧!)
排列 ( [file] => /home/lufigueroa/Desktop/test.php [line] => 12 [function] =>呼叫 [args] =>数组 ( [0] =>露西亚 [1] => PHP ) )
我最喜欢的方式,在一行!
debug_backtrace()[1]['function'];
你可以像这样使用它:
echo 'The calling function: ' . debug_backtrace()[1]['function'];
请注意,这只与去年发布的PHP版本兼容。 但出于安全原因,最好让PHP保持最新状态。
从php 5.4开始,你可以使用
$dbt=debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,2); $caller = isset($dbt[1]['function']) ? $dbt[1]['function'] : null;
这不会浪费内存,因为它忽略了参数,只返回最后2个回溯堆栈条目,并且不会在这里生成其他答案。
对我来说, debug_backtrace
已经达到了我的内存限制,我想在生产中使用它来logging和发送错误。
相反,我发现这个出色的解决scheme!
// 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}"
我只是写了一个名为“get_caller”的版本,我希望它有帮助。 我很懒惰。 你可以从一个函数运行get_caller(),你不必像这样指定它:
get_caller(__FUNCTION__);
下面是一个充满古怪的testing用例的脚本:
<?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()调用了调用get_caller()的女士()。 get_caller()不知道是谁调用的,因为女人()很谨慎,并没有告诉它,所以它回溯发现。 然后它返回谁称为女人()。 在浏览器中以源代码模式打印输出显示function堆栈:
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行!)。因为我只关心调用函数和类,所以我使用了这个小小的解决scheme:
$callers=debug_backtrace(); foreach($callers as $call) { echo "<br>" . $call['class'] . '->' . $call['function']; }
获取父函数名最简单的方法是:
$caller = next(debug_backtrace())['function'];
我所看到的这个问题的最佳答案是:
list(, $caller) = debug_backtrace(false);
短而干净