Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • 61
    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) Commented Feb 17, 2010 at 22:17
  • Many solutions seen on the web get the second element of the backtrace array to get the instance caller: can we be so sure about it? Is the second element always the one we are searching for? I thought a __construct() which includes inside another call such as parent::__construct() could shift of another position the real caller (did not tried yet). Commented Aug 30, 2011 at 10:10
  • 1
    I tried checking the order of the callers returned while using a ReflectionClass, and it obviously changes the position of the "real" caller method, which is visible in the user interface, so no assumption on the backtrace position can be made. Commented Aug 30, 2011 at 10:28
  • 4
    array shift will remove first element and return the removed element. The original array will be modified and this should give the required result echo 'called by '.$trace[0]['function'] Commented Jun 20, 2012 at 7:44
  • 24
    debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']; to get caller name with better performmance. Commented Apr 8, 2015 at 7:19