Get name of caller function in PHP?


Translate

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


All Answers
  • Translate

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

    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']}";
    

  • Translate

    Xdebug provides some nice functions.

    <?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

    sudo aptitude install php5-xdebug
    

    You might need to install php5-dev first

    sudo aptitude install php5-dev
    

    more info


  • Translate

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


  • Translate

    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.


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

    Works since 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).


  • Translate

    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;
        }   
    }
    

  • Translate

    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:

    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..
    }
    

  • Translate

    You can extract this information from the array returned by debug_backtrace


  • Translate

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


  • Translate

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


  • Translate

    This should work:

    $caller = next(debug_backtrace())['function'];