Use get_error_handler() in php 8.5

Created on 26 May 2025, 10 days 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

  • Issue created by @mstrelan
  • Merge request !12233Resolve #3526515 "Get error handler" → (Open) created by mstrelan
  • Pipeline finished with Success
    10 days ago
    Total: 635s
    #506390
  • 🇮🇹Italy mondrake 🇮🇹

    Added inline comments

  • 🇦🇺Australia mstrelan

    Thanks for the review. I initially thought about addressing the missing return and the type hints but figured it was out of scope, since arguably we don't necessarily need to change the existing set_error_handler calls in the first place. The reference would make sense to address though. If we are going to refactor the anonymous function it would probably make sense to make it a static method on the Error class, since we're repeating it 4 times.

Production build 0.71.5 2024