Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I just wrote a version of this called "get_caller", I hope it helps. Mine is pretty lazy. You can just run get_caller() from a function, you don't have to specify it like this:</p> <pre><code>get_caller(__FUNCTION__); </code></pre> <p>Here's the script in full with a quirky test case:</p> <pre><code>&lt;?php /* This function will return the name string of the function that called $function. To return the caller of your function, either call get_caller(), or get_caller(__FUNCTION__). */ function get_caller($function = NULL, $use_stack = NULL) { if ( is_array($use_stack) ) { // If a function stack has been provided, used that. $stack = $use_stack; } else { // Otherwise create a fresh one. $stack = debug_backtrace(); echo "\nPrintout of Function Stack: \n\n"; print_r($stack); echo "\n"; } if ($function == NULL) { // We need $function to be a function name to retrieve its caller. If it is omitted, then // we need to first find what function called get_caller(), and substitute that as the // default $function. Remember that invoking get_caller() recursively will add another // instance of it to the function stack, so tell get_caller() to use the current stack. $function = get_caller(__FUNCTION__, $stack); } if ( is_string($function) &amp;&amp; $function != "" ) { // If we are given a function name as a string, go through the function stack and find // it's caller. for ($i = 0; $i &lt; count($stack); $i++) { $curr_function = $stack[$i]; // Make sure that a caller exists, a function being called within the main script // won't have a caller. if ( $curr_function["function"] == $function &amp;&amp; ($i + 1) &lt; count($stack) ) { return $stack[$i + 1]["function"]; } } } // At this stage, no caller has been found, bummer. return ""; } // TEST CASE function woman() { $caller = get_caller(); // No need for get_caller(__FUNCTION__) here if ($caller != "") { echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n"; } else { echo "no-one called ", __FUNCTION__, "()\n"; } } function man() { // Call the woman. woman(); } // Don't keep him waiting man(); // Try this to see what happens when there is no caller (function called from main script) //woman(); ?&gt; </code></pre> <p>man() calls woman(), who calls get_caller(). get_caller() doesn't know who called it yet, because the woman() was cautious and didn't tell it, so it recurses to find out. Then it returns who called woman(). And the printout in source-code mode in a browser shows the function stack:</p> <pre><code>Printout of Function Stack: Array ( [0] =&gt; Array ( [file] =&gt; /Users/Aram/Development/Web/php/examples/get_caller.php [line] =&gt; 46 [function] =&gt; get_caller [args] =&gt; Array ( ) ) [1] =&gt; Array ( [file] =&gt; /Users/Aram/Development/Web/php/examples/get_caller.php [line] =&gt; 56 [function] =&gt; woman [args] =&gt; Array ( ) ) [2] =&gt; Array ( [file] =&gt; /Users/Aram/Development/Web/php/examples/get_caller.php [line] =&gt; 60 [function] =&gt; man [args] =&gt; Array ( ) ) ) man() called woman(). No surprises there. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload