Print PHP Call Stack

前端 未结 15 1634
清歌不尽
清歌不尽 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:40

    I have adapted Don Briggs's answer above to use internal error logging instead of public printing, which may be your big concern when working on a live server. Also, added few more modifications like option to include full file path instead of basic name (because, there could be files with same name in different paths), and also (for those who require it) a complete node stack output:

    class debugUtils {
        public static function callStack($stacktrace) {
            error_log(str_repeat("=", 100));
            $i = 1;
            foreach($stacktrace as $node) {
                // uncomment next line to debug entire node stack
                // error_log(print_r($node, true));
                error_log( $i . '.' . ' file: ' .$node['file'] . ' | ' . 'function: ' . $node['function'] . '(' . ' line: ' . $node['line'] . ')' );
                $i++;
            }
            error_log(str_repeat("=", 100));
        } 
    }
    
    // call debug stack
    debugUtils::callStack(debug_backtrace());
    

提交回复
热议问题