[php-src] Issue #21107: Autoloaded class missing implements/extends cannot be loaded again with require_once/will result in different class to be loaded

[email protected] (kkmuffme)
Newsgroups php.bugs
Message-ID <uw1q4PqAnloVg72od46D64MPN3dzlOYJqn5T3IDfTqQ@main.internal.php.net>
Issue: https://github.com/php/php-src/issues/21107
Author: kkmuffme

### Description

The following code:

```php
<?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

```plain
8.5
```

### Operating System

_No response_
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.