tags:

views:

5734

answers:

5

Is there a PHP function to find out the name of the caller function in a given function?

+4  A: 

debug_backtrace() supplies details of parameters, function/method calls in the current call stack.

CMS
+21  A: 

See debug_backtrace - this can trace your call stack all the way to the top

Here's how you'd get your caller

$trace=debug_backtrace();
$caller=array_shift($trace);

echo 'called by '.$caller['function']
if (isset($caller['class']))
    echo 'in '.$caller['class'];
Paul Dixon
It seems to me that this prints the callee function name. Use `list(, $caller) = debug_backtrace(false);` to get caller, `false` for performance ;-) (php5.3)
Znarkus
+1  A: 

You can extract this information from the array returned by debug_backtrace

Richard Turner
A: 

don't you need a semicolen after: echo 'called by '.$caller['function']

???

ed
A: 

Hi guys,

I've made a small extension for Zend_Log, now it shows class/method/line where logger was called from. See here http://mikebevz.com/2010/08/logger-with-caller-class-based-on-zend_log/

Mike Bevz