tags:

views:

115

answers:

2

I have a function that is often called inside of other functions, and I'd like to be able to find out automatically what the name of the referring function (if any) was.

Something like this:

function do_something()
{
    do_something_else();
}

function do_something_else()
{
    echo referring_function(); // prints 'do_something'
}

Is there any easy way to do this? Note that I know it can be done manually by passing the name as a parameter, but I'd like to know if there's a simpler approach to this. Also, I'm not looking for the __FUNCTION__ constant, as that returns the name of the function in which it is called. I want the name of the function that called the current function.

+4  A: 

You could abuse debug_backtrace() horribly to do this. Seems like there should be a better way... but I'm not thinking of it.

It seems kind of weird that you'd want to do this. If you have a real need for a function to know who is calling it, you probably have a design problem.

However, if you're sure you want to do it, you could just define a little global function like this:

function callerId(){
         $trace = debug_backtrace();
         return $trace[2]['function'];
}

That will always return the name of the function that called the function that called it. Unless, of course, the function that calls callerId() was called from the top-level script.

So, a usage example:

function callerId(){
  $trace = debug_backtrace();
  return $trace[2]['function'];
}

function do_something(){
  do_something_else();
}

function do_something_else(){
  $who_called = callerId();
}
timdev
+1 for the part about design problem...
Cellfish
Thanks for your solution. Can you please elaborate why this might be a design problem?
Josh Leitzel
It implies that you're not structuring things cleanly. A function should abstract some specific process, and generally speaking, should communicate with the "outside" only through the arguments it is passed (input), and the value it returns. A more common "red flag" is having a function rely on some global variable. While there can be exceptions when global variables are concerned, I have a hard time imagining a situation where the call stack would contain information would be appropriate. That sort of information is "magic", and should be discouraged. Temporary debug code doesn't count.
timdev
I understand what you mean. I'm not actually using this for anything (yet), but I was thinking it could be useful for an error function. It would be able to say things like "Error in {function}: file passed does not exist." There is probably a better way to do this, though, but in all honesty I haven't delved deep enough into error handling to know of it.
Josh Leitzel
A: 

Maybe force the calling function to throw in its name into one of the function arguments. Something like:

function do_something()
{
    do_something_else(__FUNCTION__);
}

function do_something_else($calling)
{
    echo $calling; // prints 'do_something'
}

Untested though.

Lukman
I specifically said that I didn't want to enter it manually and I didn't want to use __FUNCTION__.
Josh Leitzel