This is more for my own personal edification than anything else but, this is something that has always bothered me: Why specifically can’t PHP perform “autoloading” while in CLI mode?
I’ve been reading this disclaimer for years, but I’ve never read anything that touches on why:
http://php.net/manual/en/language.oop5.autoload.php:
Note: Autoloading is not available if using PHP in CLI interactive mode.
Does anyone know what is preventing PHP, as a language, from autoloading while working in CLI mode?
Autoloading on the command line works. Do note the mention of “interactive”.
PHP comes with two interactive modes, but unfortunately both of them are invoked by using
php -aon your command shell.If PHP is compiled with readline support, you get the “interactive shell”. In this mode, every command is evaluated nearly instantly, and you also get instant feedback about any parsing errors.
In this mode, autoloading works.
The other mode is called “interactive mode”. This mode is void of any fancy stuff, it only emits a short message, and then you basically write a PHP script – and nothing gets done unless you close the STDIN. Only then the written code gets parsed and executed. And this is the only case where the
__autoload()function is not called for unknown classes.Example for an interactive shell session (using PHP 5.3.2 on Linux):
Example for an interactive mode (using PHP 5.3.18 on Windows)