How can I tell what function called my PHP function?

Sometimes you need to know what function called the PHP function you are currently in.

debug_backtrace is your friend here, a handy PHP function that accesses the stack of function calls that led the PHP engine from the first line of index.php to where it is now.

Sadly, the stack trace can get a bit deep, so if you just want to know what function called the function you are in then try this:

 print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1));`

What you should get is just the last step in the stack trace, the nice tidy array that will look something like this…

[0] => Array
[file] => /var/www/html/CWL/classes/db.php
[line] => 186
[function] => validate
[class] => user
[type] => ->

Leave a Reply