为什么auto_prepend_file在php的交互模式下不起作用? [英] Why auto_prepend_file take no effect in php's interactive mode?

查看:55
本文介绍了为什么auto_prepend_file在php的交互模式下不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过composer安装软件包并导入它:

Install package via composer and import it :

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

将其加载到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');

显示项目目录结构:

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 包.

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

为什么auto_prepend_file在php的交互模式下不起作用?

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

推荐答案

auto_prepend_file 确实可以在交互式shell中使用.问题是 use 关键字仅对当前行有效.

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

使用此prepend.php文件:

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

这也起作用(在同一行上 use new ):

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

此操作失败:


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

这篇关于为什么auto_prepend_file在php的交互模式下不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆