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

橙三吉。 提交于 2021-02-10 05:12:19

问题


Install package via composer and import it :

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

Load it in the index.php:

cd public
vim  index.php
<?php

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

Display the project directory structure:

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

Verify it in browser 127.0.0.1/myproject/public,it works fine,the package Megting was loaded.

Now ,i want to load it in interactive mode:

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

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


回答1:


auto_prepend_file does work in interactive shell. The issue is that the use keyword has an effect only on the current line.

With this prepend.php file:

<?php
namespace foo;

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

This works (full class name):

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

php > new foo\Bar();
Success

This also works (use and new on the same line):

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

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

This fails:

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


来源:https://stackoverflow.com/questions/63311320/why-auto-prepend-file-take-no-effect-in-phps-interactive-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!