Skip to content

Autoloader doesn't work in header_register_callback() callback when an exception is being thrown #20619

@tgr

Description

@tgr

Description

The following code:

<?php

spl_autoload_register( function () {
        class Autoloaded {}
} );

header_register_callback( function () {
	new Autoloaded();
} );

throw new Exception();

Resulted in this output:

PHP Fatal error:  Uncaught Error: Class "Autoloaded" not found in /path/to/foo.php:8
Stack trace:
#0 [internal function]: {closure:/path/to/foo.php:7}()
#1 {main}
  thrown in /path/to/foo.php on line 8

But I expected this output instead:

Fatal error: Uncaught Exception in /path/to/foo.php:11
Stack trace:
#0 {main}
  thrown in /path/to/foo.php on line 11

Seen on Wikipedia servers (T261260) first on PHP 7.4 then 8.1, reported on various other versions. Could reproduce locally on 8.5. Can't reproduce it on 3v4l.org for some reason.

PHP Version

PHP 8.5.0 (cli) (built: Nov 20 2025 19:23:30) (NTS)
Copyright (c) The PHP Group
Built by Debian
Zend Engine v4.5.0, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.0, Copyright (c), by Zend Technologies

Operating System

Ubuntu 24.04

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions