gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:11:22 25 4
gpt4 key购买 nike

通过 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';
}
}
?>

这有效(全类名):

php -d auto_prepend_file=prepend.php -aInteractive shellphp > new foo\Bar();Success

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

php -d auto_prepend_file=prepend.php -aInteractive shellphp > use foo\Bar; new Bar();Success

这失败了:

php -d auto_prepend_file=prepend.php -aInteractive shellphp > use foo\Bar;php > new Bar();PHP Warning:  Uncaught Error: Class 'Bar' not found in php shell code:1

关于php - 为什么 auto_prepend_file 在 php 的交互模式下不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63311320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com