How to get name of calling function/method in PHP ?

Why need to get the name of calling function?
The code consist of multiple functions performing different tasks but associated with each other directly or indirectly, and suddenly display an error in some of the functions then it will become necessary to find the name of the function in which an error has been occurred.

Approach:

  • Declare a user defined function CallingFunctionName() which will act as debugger function (It trace the other function in a script).
  • Create the reference of the Exception class using a ‘new’ keyword.
  • Now using this reference variable call the inbuilt function getTrace() (which is the member function of Exception class).
  • Fetch the value returned by getTrace() function in any variable (getTrace() function returns the associative array).
  • Print that variable using print_r() function.
  • Create one or more user defined functions to test the tracing function.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to get the name
// of calling function/method
  
function CallingFunctionName() {
      
    // Create an exception
    $ex = new Exception();
      
    // Call getTrace() function
    $trace = $ex->getTrace();
      
    // Position 0 would be the line
    // that called this function
    $final_call = $trace[1];
      
    // Display associative array 
    print_r($final_call);
}
  
// Declare firstCall() function
function firstCall($x, $y) {
      
    // Call secondCall() function
    secondCall($x, $y);
}
  
// Declare secondCall() function
function secondCall($x, $y) {
      
    // Call CallingFunctionName()
    // function
    CallingFunctionName();
}
  
// Call firstCall() function
firstCall('test', 'php');
  
?>

chevron_right


Output:

Array
(
    [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
    [line] => 26
    [function] => secondCall
    [args] => Array
        (
            [0] => test
            [1] => php
        )

)

Explanation: The getTrace() function will scan whole method callstack and stores all the information in the form of an associative array. Associative array carries the following information.

  • [file]: Absolute path of the current PHP file.
  • [line]: Line number where the function ‘CallingFunctionName()’ has been called.
  • [function]: Carries the name of calling function.
  • [args]: Gives the values of arguments that calling the function.

On the line number 15, pass the value 1 as subscript of trace[], so it returns all information of upper most function in the stack.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to get the name
// of calling function/method
  
function CallingFunctionName() {
      
    // Create an exception
    $ex = new Exception();
      
    // Call getTrace() function
    $trace = $ex->getTrace();
      
    // Position 0 would be the line
    // that called this function
    $final_call = $trace[2];
      
    // Display associative array 
    print_r($final_call);
}
  
// Declare firstCall() function
function firstCall($x, $y) {
      
    // Call secondCall() function
    secondCall($x, $y);
}
  
// Declare secondCall() function
function secondCall($x, $y) {
      
    // Call CallingFunctionName()
    // function
    CallingFunctionName();
}
  
// Call firstCall() function
firstCall('test', 'php');
  
?>

chevron_right


Output:

Array
(
    [file] => /home/1b5518d5af0615813238e7534ccb6e8a.php
    [line] => 38
    [function] => firstCall
    [args] => Array
        (
            [0] => test
            [1] => php
        )

)

Explanation: The output contains all the information of function firstcall() because the value of subscript trace is 2.

  • trace[0]: Position 0 would be the function itself (here CallingFunctionName()).
  • trace[1]: Position 1 would be the function secondcall because this is the top most function in callstack.
  • trace[2]: Position 2 would be the function ‘firstcall’ because this is the top 1 function in callstack.
  • trace[n]: Position n would be the nth function because this is the top nth function in callstack.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.