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.
- 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.
Array ( [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php [line] => 26 [function] => secondCall [args] => Array (  => test  => 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.
Array ( [file] => /home/1b5518d5af0615813238e7534ccb6e8a.php [line] => 38 [function] => firstCall [args] => Array (  => test  => php ) )
Explanation: The output contains all the information of function firstcall() because the value of subscript trace is 2.
- trace: Position 0 would be the function itself (here CallingFunctionName()).
- trace: Position 1 would be the function secondcall because this is the top most function in callstack.
- trace: 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.
- Calling A Super Class Constructor in Scala
- Java Interoperability - Calling Java from Kotlin
- Java Interoperability - Calling Kotlin from Java
- Calling a Super Class Constructor in Python
- How to get file name from a path in PHP?
- How to get current function name in PHP?
- How to get a variable name as a string in PHP?
- How to get the function name inside a function in PHP ?
- Get name of current method being executed in Java
- How to get file input by selected file name without path using jQuery ?
- How to get city name by using geolocation ?
- How to convert number to month name in PHP?
- PHP | Get PHP configuration information using phpinfo()
- How to find the parent class name with known class in jQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.