关于spl_autoload()作为__autoload()默认实现的相关疑问
__autoload() and spl_autoload() in PHP’s Autoloading System Great question—let’s unpack this confusion step by step, since the relationship between these two functions is a common point of mix-up for PHP developers.
1. Does __autoload() have a default implementation if we don’t define it?
Short answer: No. PHP does not provide a built-in default implementation for __autoload(). If you never explicitly define a __autoload() function in your code, PHP will throw a fatal error the moment you try to use an unloaded class—there’s no automatic fallback logic baked into __autoload() itself.
The confusion comes from how spl_autoload() interacts with the SPL autoload stack. When you call spl_autoload_register() without passing a custom callback function, PHP automatically registers spl_autoload() as the default autoloader. This is what sources mean when they say spl_autoload() is the "default implementation"—it’s the default for the SPL autoload mechanism, not for the old __autoload() function.
2. Is spl_autoload() an internal function, and why does it exist?
Yes, spl_autoload() is a core internal function provided by PHP’s Standard PHP Library (SPL). It exists to solve a major limitation of the original __autoload():
__autoload()can only be defined once in your codebase. If you’re using multiple libraries that each need their own autoloading logic, you can’t have multiple__autoload()functions—defining a second one will overwrite the first.- The SPL autoload system (built around
spl_autoload_register()) fixes this by letting you register a stack of autoload functions. Each function in the stack gets a chance to load the class until one succeeds.
spl_autoload() acts as a simple, generic fallback for this stack. Its logic is straightforward:
- It converts the class name to lowercase (by default)
- It appends a file extension (
.phpby default, though you can change this withspl_autoload_extensions()) - If the class uses namespaces, it replaces namespace separators (
\) with directory separators (e.g.,\MyNamespace\MyClassbecomesmynamespace/myclass.php) - It then tries to include the resulting file
3. How do __autoload() and spl_autoload() interact?
- If you define
__autoload()but never usespl_autoload_register(), PHP will use your custom__autoload()when it needs to load an unloaded class. - If you call
spl_autoload_register()without arguments, PHP addsspl_autoload()to the autoload stack. In this case,__autoload()will not be used unless you explicitly register it withspl_autoload_register('__autoload'). - Once you register any functions with
spl_autoload_register(), the SPL stack takes precedence over the standalone__autoload()function.
Quick Example
Suppose you have a class MyClass in myclass.php:
// If you do this: spl_autoload_register(); // Registers spl_autoload() by default $obj = new MyClass(); // Works! spl_autoload() finds and includes myclass.php // But if you do this without spl_autoload_register(): $obj = new MyClass(); // Fatal error: Class 'MyClass' not found (since __autoload() isn't defined)
内容的提问来源于stack exchange,提问作者DoeJane

