Alex Rivera | Logout

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

Asked 2010-01-21T16:10:17.390
308

I am aware of function debug_backtrace, but I am looking for some ready to use implementation of function like GetCallingMethodName()? It would be perfect if it gave method's class too (if it is indeed a method).

Edit
Report

1 Answer

708

The simplest way is:

echo debug_backtrace()[1]['function'];

As noted in the comments below, this can be further optimized by passing arguments to:

  • omit both the object and args indices
  • limit the number of stack frames returned
echo debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,2)[1]['function'];
answered 2012-06-28T04:24:57.763

Your Answer