How can I get PHP to produce a backtrace upon errors?

后端 未结 11 2132
無奈伤痛
無奈伤痛 2020-11-28 23:29

Trying to debug PHP using its default current-line-only error messages is horrible. How can I get PHP to produce a backtrace (stack trace) when errors are produced?

11条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 00:26

    PHP DeBugger also does a back trace similiar to PHP Error with more options.
    If you want you can easily make your own with set_error_handler and debug_backtrace

    set_error_handler ($error_handler, error_reporting);
    /**
     * @var int $errno the error number
     * @var string $errstr the error message
     * @var string $errfile the error file
     * @var int $errline the line of the error
     */
    $error_handler = function($errno, $errstr, $errfile, $errline){
        $trace = debug_backtrace();
        array_shift($backtrace);//remove the stack about this handler
        foreach($trace as $k => $v){
            //parse your backtrace
        }
    }
    

    Also note that for internal stacks in the backtrace some of the keys will not be set. Be sure to check if the key exist before you do something with it if you have all errors on :)

提交回复
热议问题