This RFC enables invoking of destructor methods after a fatal error (such as an execution timeout), to match the behavior of shutdown handlers, which are also always executed after a fatal error.
This code will always work correctly under the new RFC:
final class StatCounter { private static self $instance; public static function get(): self { return self::$instance ??= new self; } private array $stats = []; private function __construct() {} public function __destruct() { // I.e. using influx/telegraf echo "Sending stats: ".json_encode($this->stats)."\n"; } public function inc(string $key, int $by = 1): void { $this->stats[$key] ??= 0; $this->stats[$key] += $by; } } StatCounter::get()->inc('test'); StatCounter::get()->inc('test2'); // Stat reporting will always work on shutdown even if the following two lines are uncommented //set_time_limit(1); //while (1);
None.
PHP 8.5
PHP code will be able to reliably use destructors to execute code on shutdown if needed, matching the behavior of the shutdown handler.