Why doesn't PHP Autoload function work in CLI mode?

This is more for my personal edification than anything else, but this is what has always bothered me: why specifically PHP can not perform “autoload” in CLI mode?

I have read this disclaimer for many years, but I have never read anything that affects why:

http://php.net/manual/en/language.oop5.autoload.php :

Note. Autoload is not available when using PHP in CLI interactive mode.

Does anyone know what will prevent PHP, as a language, from autoload when working in CLI mode?

+5
source share
3 answers

Startup works on the command line. Pay attention to the mention of "interactive."

PHP , , , php -a .

PHP readline, " ". , .

.

" ". - , , PHP script - , STDIN. . , __autoload() .

( PHP 5.3.2 Linux):

vagrant@lucid32:/etc/apache2$ php -a
Interactive shell

php > function __autoload($classname) {
php { echo "Autoload $classname";
php { eval("class $classname{}");
php { return true;
php { }
php > new Bar();
Autoload ▒▒Bar
php > new FooBar();
Autoload ▒▒FooBar
php > var_dump($a = get_declared_classes());
array(123) {
[0]=>
string(8) "stdClass"
[1]=>
string(9) "Exception"
[2]=>
string(14) "ErrorException"
   ... lots of internal classes here ...
[121]=>
string(3) "Bar"
[122]=>
string(6) "FooBar"
}
php >

( PHP 5.3.18 Windows)

PS C:\Users\sven> php -a
Interactive mode enabled

<?php
function __autoload($class) { echo "Auto: $class"; eval("class $class {}"); }
echo "Hello World";
$x = new Foo;
var_dump($x);
var_dump($a = get_declared_classes());
^Z
Hello World
Fatal error: Class 'Foo' not found in - on line 4

Call Stack:
  100.6337    1114608   1. {main}() -:0
+4

PHP- CLI : PHP, script, PHP://stdin, . , , . __FILE__, .
, , ( ), , . , CLI:

$ php '<?php echo "this is read from STDIN"; ?>'

:

args...                                           script. Use - args,                             - script stdin

+3

I would say that they are not talking about the CLI, they are talking about the interactive mode of PHP, aka php -a.

And why? Because it is only for testing purposes and short snipplets, and if ANYTHING is automatically loaded, the behavior can be crazy.

http://www.php.net/manual/en/features.commandline.interactive.php

+1
source

All Articles