-
Notifications
You must be signed in to change notification settings - Fork 8k
Description
Description
The following code:
<?php
spl_autoload_register( function ( $class_name ) {
if ( $class_name === 'Foo' ) {
echo "ONE" . PHP_EOL;
require_once __DIR__ . '/foo.php';
}
} );
spl_autoload_register( function ( $class_name ) {
if ( $class_name === 'Foo' ) {
echo "TWO" . PHP_EOL;
require __DIR__ . '/fake-foo.php';
}
} );
try {
class_exists( Foo::class );
} catch ( Throwable $e ) {
echo 'catch' . PHP_EOL;
}
try {
class_exists( Foo::class );
} catch ( Throwable $e ) {
echo 'catch' . PHP_EOL;
}foo.php
<?php
echo "foo.php" . PHP_EOL;
class Foo extends Bar {}
fake-foo.php
<?php
echo "fake-foo.php" . PHP_EOL;
class Foo {}
Resulted in this output:
ONE
foo.php
catch
ONE
TWO
fake-foo.php
But I expected this output instead:
Up to discussion
It's unexpected that the class of the file is not added to the loaded classes (possibly at least as incomplete class? https://www.php.net/manual/en/class.php-incomplete-class.php) but the file it is located in is.
This leads to unxpected/inconsistent runtime behavior.
Perhaps the file the class is located in should not be added to the loaded files either?
Or the class that failed to load should be added as existing class but incomplete?
Or perhaps any classes it extends/implements (and trait use) should not be autoloaded yet but only when the class is instantiated/used?
Or perhaps treat include/require_once in autoloader like require/include?
Or anything else?
At least, the documentation for spl_autoload_register needs to be updated to clearly mention that it should not use include_once/require_once to load classes, since it can lead to unexpected behavior.
PHP Version
8.5
Operating System
No response