Fatal Errors Now Include Stack Trace in PHP 8.5

Fatal Errors Now Include Stack Trace in PHP 8.5

Stack traces are useful for testing and debugging purposes. They provide a detailed snapshot of the call sequence leading up to an error, including the function names and the parameters passed in each stack frame. By examining a stack trace, developers can quickly pinpoint where a problem occurred and understand the sequence of events that led to it, making troubleshooting far more efficient.

Consider the following example:

<?php

ini_set('max_execution_time', '1');

function longRunningFunction(): void
{
    sleep(2);
}

longRunningFunction();

The example causes a fatal error because it sets max_execution_time to 1, and longRunningFunction runs longer than the limit.

Before PHP 8.5, the script would terminate with the following message:

Fatal error: Maximum execution time of 1 second exceeded in ...\main.php on line 7

The error message shows the file and line number, but it doesn't include a stack trace. Without the stack trace, it's harder to see the sequence of function calls that led to the error.

Since PHP 8.5, fatal errors include stack trace.

Fatal error: Maximum execution time of 1 second exceeded in ...\main.php on line 7
Stack trace:
#0 ...\main.php(7): sleep(2)
#1 ...\main.php(10): longRunningFunction()
#2 {main}

This makes it much easier for developers to trace the exact flow of execution and identify the root cause of the error quickly.

The stack trace for fatal errors can be disabled by setting the fatal_error_backtraces INI directive in the configuration file (php.ini).

fatal_error_backtraces = Off

Leave a Comment

Cancel reply

Your email address will not be published.