为什么auto_prepend_file在php的交互模式下不生效?

Why auto_prepend_file take no effect in php's interactive mode?

通过 composer 安装包并导入它:

mkdir  myproject
cd myproject
composer require metowolf/meting
mkdir public
touch public/index.php

将其加载到 index.php:

cd public
vim  index.php
<?php

require __DIR__ . '/../vendor/autoload.php';
use Metowolf\Meting;
$api = new Meting('netease');

显示项目目录结构:

tree myproject
myproject
├── composer.json
├── composer.lock
├── public
│   └── index.php
└── vendor
    ├── autoload.php
    ├── composer
    │   ├── autoload_classmap.php
    │   ├── autoload_namespaces.php
    │   ├── autoload_psr4.php
    │   ├── autoload_real.php
    │   ├── autoload_static.php
    │   ├── ClassLoader.php
    │   ├── installed.json
    │   └── LICENSE
    └── metowolf
        └── meting
            ├── composer.json
            ├── LICENSE
            ├── README.md
            └── src
                └── Meting.php

在浏览器中验证 127.0.0.1/myproject/public,它工作正常,包 Megting 已加载。

现在,我想以交互模式加载它:

php  -d auto_prepend_file=/home/debian/myproject/vendor/metowolf/meting/src/Meting.php  -a
Interactive mode enabled
php > use Metowolf\Meting;
php > $api = new Meting('netease');
PHP Warning:  Uncaught Error: Class 'Meting' not found in php shell code:1
Stack trace:
#0 {main}
  thrown in php shell code on line 1

为什么auto_prepend_file在php的交互模式下没有生效?

auto_prepend_file 在交互式 shell 中确实有效。问题是 use 关键字仅对当前行有效。

有了这个 prepend.php 文件:

<?php
namespace foo;

class Bar
{
    function __construct()
    {
        echo 'Success';
    }
}
?>

这有效(class 全名):

php -d auto_prepend_file=prepend.php -a
Interactive shell

php > new foo\Bar();
Success

这也有效(usenew 在同一行):

php -d auto_prepend_file=prepend.php -a
Interactive shell

php > use foo\Bar; new Bar();
Success

这失败了:

php -d auto_prepend_file=prepend.php -a
Interactive shell

php > use foo\Bar;
php > new Bar();
PHP Warning:  Uncaught Error: Class 'Bar' not found in php shell code:1