Print PHP Call Stack

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

    If you want a stack trace which looks very similar to how php formats the exception stack trace than use this function I wrote:

    function debug_backtrace_string() {
        $stack = '';
        $i = 1;
        $trace = debug_backtrace();
        unset($trace[0]); //Remove call to this function from stack trace
        foreach($trace as $node) {
            $stack .= "#$i ".$node['file'] ."(" .$node['line']."): "; 
            if(isset($node['class'])) {
                $stack .= $node['class'] . "->"; 
            }
            $stack .= $node['function'] . "()" . PHP_EOL;
            $i++;
        }
        return $stack;
    } 
    

    This will return a stack trace formatted like this:

    #1 C:\Inetpub\sitename.com\modules\sponsors\class.php(306): filePathCombine()
    #2 C:\Inetpub\sitename.com\modules\sponsors\class.php(294): Process->_deleteImageFile()
    #3 C:\Inetpub\sitename.com\VPanel\modules\sponsors\class.php(70): Process->_deleteImage()
    #4 C:\Inetpub\sitename.com\modules\sponsors\process.php(24): Process->_delete() 
    

提交回复
热议问题