Clarify require might be preferable over require_once for autoloaders
Nobody has claimed this yet.
- Dominant language
- XML
- Stars
- 596
- Forks
- 890
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 55
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
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the documentation for spl_autoload_register and compare its guidance with the PHP 8.5 reproducer in the issue, including the require_once and require behavior. Clarify the documented warning about include_once/require_once in autoloaders and ensure the resulting text matches the demonstrated runtime behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100