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

11 Jan, 2019
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() functionfunction firstCall($x, $y) {          // Call secondCall() function    secondCall($x, $y);}  // Declare secondCall() functionfunction secondCall($x, $y) {          // Call CallingFunctionName()    // function    CallingFunctionName();}  // Call firstCall() functionfirstCall('test', 'php');  ?>
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() functionfunction firstCall($x, $y) {          // Call secondCall() function    secondCall($x, $y);}  // Declare secondCall() functionfunction secondCall($x, $y) {          // Call CallingFunctionName()    // function    CallingFunctionName();}  // Call firstCall() functionfirstCall('test', 'php');  ?>
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.
Picked
Technical Scripter 2018
PHP
PHP Programs
Technical Scripter
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
How to pass JavaScript variables to PHP ?
Split a comma delimited string into an array in PHP
Download file from URL using PHP
How to convert array to string in PHP ?
How to call PHP function on the click of a Button ?
How to pass JavaScript variables to PHP ?
Split a comma delimited string into an array in PHP
How to get parameters from a URL string in PHP?