Why doesn't PHP's Autoload feature work in CLI mode?

后端 未结 3 1755
無奈伤痛
無奈伤痛 2021-01-17 22:46

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

相关标签:
3条回答
  • 2021-01-17 22:55

    The way to think about PHP's interactive CLI is basically: PHP starting an empty script, and reading a file from PHP://stdin, which it then parses and executes. The file location, and therefore include path, and other environment variables are ignored. The current __FILE__, if you will does not exist.
    Well, that's just a way of looking at it, it doesn't tell the whole story (far from it), but in practice, that's the way you can think of it. There needn't be a file to start in interactive CLI, at all:

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

    As can be deduced from the docs:

    args...                    Arguments passed to script. Use -- args when first argument                               starts with - or script is read from stdin

    0 讨论(0)
  • 2021-01-17 22:55

    I would say, they aren't talking about CLI they are talking about PHP interactive mode, aka php -a.

    And why? Because it is only for testing purposes and short snipplets and if ANYTHING is autoloaded the behavior might be crazy.

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

    0 讨论(0)
  • 2021-01-17 23:07

    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 -a on 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):

    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 >
    

    Example for an interactive mode (using PHP 5.3.18 on 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
    
    0 讨论(0)
提交回复
热议问题