在 PHP 中获取调用函数的名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/190421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:52:21  来源:igfitidea点击:

Get name of caller function in PHP?

php

提问by Paul Dixon

Is there a PHP function to find out the name of the caller function in a given function?

是否有 PHP 函数可以找出给定函数中调用者函数的名称?

回答by Paul Dixon

See debug_backtrace- this can trace your call stack all the way to the top.

请参阅debug_backtrace- 这可以将您的调用堆栈一直跟踪到顶部。

Here's how you'd get your caller:

以下是您获得来电者的方式:

$trace = debug_backtrace();
$caller = $trace[1];

echo "Called by {$caller['function']}";
if (isset($caller['class']))
    echo " in {$caller['class']}";

回答by svassr

Xdebug provides some nice functions.

Xdebug 提供了一些不错的功能。

<?php
  Class MyClass
  {
    function __construct(){
        $this->callee();
    }
    function callee() {
        echo sprintf("callee() called @ %s: %s from %s::%s",
            xdebug_call_file(),
            xdebug_call_line(),
            xdebug_call_class(),
            xdebug_call_function()
        );
    }
  }
  $rollDebug = new MyClass();
?>

will return trace

将返回跟踪

callee() called @ /var/www/xd.php: 16 from MyClass::__construct

To install Xdebug on ubuntu the best way is

在 ubuntu 上安装 Xdebug 最好的方法是

sudo aptitude install php5-xdebug

You might need to install php5-dev first

您可能需要先安装 php5-dev

sudo aptitude install php5-dev

more info

更多信息

回答by MANISH ZOPE

This is very late but I would like to share the function that will give name of the function from which current function is called.

这已经很晚了,但我想分享一个函数,该函数将给出调用当前函数的函数的名称。

public function getCallingFunctionName($completeTrace=false)
    {
        $trace=debug_backtrace();
        if($completeTrace)
        {
            $str = '';
            foreach($trace as $caller)
            {
                $str .= " -- Called by {$caller['function']}";
                if (isset($caller['class']))
                    $str .= " From Class {$caller['class']}";
            }
        }
        else
        {
            $caller=$trace[2];
            $str = "Called by {$caller['function']}";
            if (isset($caller['class']))
                $str .= " From Class {$caller['class']}";
        }
        return $str;
    }

I hope this will be useful.

我希望这会很有用。

回答by CMS

debug_backtrace()supplies details of parameters, function/method calls in the current call stack.

debug_backtrace()提供当前调用堆栈中参数、函数/方法调用的详细信息。

回答by flori

echo debug_backtrace()[1]['function'];

Works since PHP 5.4.

PHP 5.4 开始工作

Or optimised (e.g. for non-debug use cases):

或优化(例如对于非调试用例):

echo debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];

The first argument prevents to populate unused function arguments, the second limits the trace to two levels (we need the second).

第一个参数防止填充未使用的函数参数,第二个将跟踪限制为两个级别(我们需要第二个)。

回答by Paul Gobée

Made this and using this myself

自己制作并使用它

/**
 * Gets the caller of the function where this function is called from
 * @param string what to return? (Leave empty to get all, or specify: "class", "function", "line", "class", etc.) - options see: http://php.net/manual/en/function.debug-backtrace.php
 */
function getCaller($what = NULL)
{
    $trace = debug_backtrace();
    $previousCall = $trace[2]; // 0 is this call, 1 is call in previous function, 2 is caller of that function

    if(isset($what))
    {
        return $previousCall[$what];
    }
    else
    {
        return $previousCall;
    }   
}

回答by lrd

I just wanted to state that flori's way won't work as a function because it will always return the called function name instead of the caller, but I don't have reputation for commenting. I made a very simple function based on flori's answer that works fine for my case:

我只是想说明 flori 的方式不能作为函数工作,因为它总是返回被调用的函数名而不是调用者,但我没有评论的声誉。我根据弗洛里的回答做了一个非常简单的函数,它适用于我的情况:

class basicFunctions{

    public function getCallerFunction(){
        return debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2]['function'];
    }

}

EXAMPLE:

例子:

function a($authorisedFunctionsList = array("b")){
    $ref = new basicFunctions;
    $caller = $ref->getCallerFunction();

    if(in_array($caller,$authorisedFunctionsList)):
        echo "Welcome!";
        return true;
    else:
        echo "Unauthorised caller!";
        return false; 
    endif;
}

function b(){
    $executionContinues = $this->a();
    $executionContinues or exit;

    //Do something else..
}

回答by Gershon Herczeg

This one worked best for me: var_dump(debug_backtrace());

这个对我来说效果最好: var_dump(debug_backtrace());

回答by Richard Turner

You can extract this information from the array returned by debug_backtrace

您可以从debug_backtrace返回的数组中提取此信息

回答by vrijdenker

Actually I think debug_print_backtrace() does what you need. http://php.net/manual/en/function.debug-print-backtrace.php

实际上我认为 debug_print_backtrace() 可以满足您的需求。 http://php.net/manual/en/function.debug-print-backtrace.php