Stuff in the ‘Debugging’ Category

Get the Calling Function or Class Method

Wednesday, June 19th, 2013

During a recent Magento project, I needed to get the calling class and method from a class that was being called multiple times. Due to the construction of Magento, the simplest way would be to use php’s debug_backtrace function. However, in this case when it was output there were over 82,000 lines of debug information. All I was concerned with was the calling function and class, so trying to wade through all of that information was rather overwhelming.

So, I whipped up this little function that takes care of things quite nicely, even formatting the output in the standard class->method structure:

        $callers=debug_backtrace();
        foreach($callers as $call) {
        	echo "
" . $call['class'] . '->' . $call['function']; }