You can severely abuse debug_backtrace() to do this. It seems like there should be a better way ... but I don't think about it.
It seems strange that you want to do this. If you have a real need for a function to know who calls it, you probably have a design problem.
However, if you are sure you want to do this, you can simply define a small global function like this:
function callerId(){ $trace = debug_backtrace(); return $trace[2]['function']; }
This will always return the name of the function that calls the function it calls. Unless, of course, the function calling callerId() was called from a top-level script.
So, an example of use:
function callerId(){ $trace = debug_backtrace(); return $trace[2]['function']; } function do_something(){ do_something_else(); } function do_something_else(){ $who_called = callerId(); }
source share