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
- Find an array of size N that satisfies the given conditions
- Designing models in Cassandra
- Online Transaction Processing (OLTP) and Online Analytic Processing (OLAP)
- Brute Force Attack
- How to play/pause video using jQuery ?
- Steps to Create and Publish NPM packages
- VBScript | Introduction
- What is Nikto and it's usages ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.