Use get_error_handler() in php 8.5

Created on 26 May 2025, about 1 month ago

Problem/Motivation

PHP 8.5 introduces get_error_handler. Before this, the only way to get the handler was to set it to something else, then restore it.

Steps to reproduce

See \Drupal\Core\Utility\Error::currentErrorHandler:

public static function currentErrorHandler(): ?callable {
  $currentHandler = set_error_handler('var_dump');
  restore_error_handler();
  return $currentHandler;
}

There are a few other places that use set_error_handler to get the current handler, but they are also intentionally setting the error handler at the same time. It would probably be clearer to use get_error_handler first.

$ grep -r " = set_error_handler" core
core/lib/Drupal/Core/Utility/Error.php:    $currentHandler = set_error_handler('var_dump');
core/lib/Drupal/Core/Render/ElementInfoManager.php:    $previous_error_handler = set_error_handler(function ($severity, $message, $file, $line) use (&$previous_error_handler) {
core/tests/Drupal/KernelTests/Core/Render/Element/DeprecatedElementTest.php:    $previous_error_handler = set_error_handler(function ($severity, $message, $file, $line) use (&$previous_error_handler) {
core/tests/Drupal/KernelTests/Core/Render/Element/DeprecatedElementTest.php:    $previous_error_handler = set_error_handler(function ($severity, $message, $file, $line) use (&$previous_error_handler) {
core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php:    $previous_error_handler = set_error_handler(function ($severity, $message, $file, $line) use (&$previous_error_handler) {

Proposed resolution

Add symfony/polyfill-php85
Update above instances to use get_error_handler

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

📌 Task
Status

Active

Version

11.0 🔥

Component

base system

Created by

🇦🇺Australia mstrelan

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024