Print PHP Call Stack

前端 未结 15 1649
清歌不尽
清歌不尽 2020-11-27 09:08

I\'m looking for a way to print the call stack in PHP.

Bonus points if the function flushes the IO buffer.

15条回答
  •  再見小時候
    2020-11-27 09:24

    Backtrace dumps a whole lot of garbage that you don't need. It takes is very long, difficult to read. All you usuall ever want is "what called what from where?" Here is a simple static function solution. I usually put it in a class called 'debug', which contains all of my debugging utility functions.

    class debugUtils {
        public static function callStack($stacktrace) {
            print str_repeat("=", 50) ."\n";
            $i = 1;
            foreach($stacktrace as $node) {
                print "$i. ".basename($node['file']) .":" .$node['function'] ."(" .$node['line'].")\n";
                $i++;
            }
        } 
    }
    

    You call it like this:

    debugUtils::callStack(debug_backtrace());
    

    And it produces output like this:

    ==================================================
     1. DatabaseDriver.php::getSequenceTable(169)
     2. ClassMetadataFactory.php::loadMetadataForClass(284)
     3. ClassMetadataFactory.php::loadMetadata(177)
     4. ClassMetadataFactory.php::getMetadataFor(124)
     5. Import.php::getAllMetadata(188)
     6. Command.php::execute(187)
     7. Application.php::run(194)
     8. Application.php::doRun(118)
     9. doctrine.php::run(99)
     10. doctrine::include(4)
    ==================================================
    

提交回复
热议问题