PHP 8.5: Fatal Error Backtraces
This is the 3rd in my mini series, exploring some of the features of PHP 8.5, which is coming in November. Previously we explored the Pipe operator and the new --diff flag for php ini:
This time we're exploring the fatal_error_backtraces setting being added. Which allows developers to see backtraces for fatal errors. Let's take this code:
class A {
public function loadClassB() {
require 'b.php';
return new B();
}
}
class B {}
class C {
public function loadClassA() {
return new A();
}
}
(new C())->loadClassA()->loadClassB();Then in another file
class B {};Currently you'd get:
Fatal error: Cannot declare class B, because the name is already in use in /app/scripts/b.php on line 3But in PHP 8.5, you'd see:
Fatal error: Cannot redeclare class B (previously declared in /app/scripts/index.php:11) in /app/scripts/b.php on line 3
Stack trace:
#0 /srv/app/index.php(6): require()
#1 /srv/app/index.php(21): A->loadClassB()
#2 {main}This new setting will be enabled by default in PHP 8.5, you are free to disable it if you want to though!