Friday, October 10, 2025
HomeLanguagesHow to get name of calling function/method in PHP ?

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:




<?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');
  
?>


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:




<?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');
  
?>


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.
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6717 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6792 POSTS0 COMMENTS