我经常发现PHP的交互模式——php -a
—非常有用,但如果我可以启动它并立即执行一些命令来初始化我的环境,那会更有用。比如运行自动加载器,设置一些use
命名空间的快捷方式等
这是一个例子:
include "../../autoloader.php";
use App/Foo/Bar as Bar;
我想也许我可以将这些行添加到文本文件中initialize.txt
然后启动交互模式php -a < initialize.txt
,但这没有用。
我怎样才能做到这一点?
As 托马斯·克里默斯提到 https://stackoverflow.com/questions/18294683/initialising-php-interactive/18294717#18294717,你必须使用自动前置文件 http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-filePHP 标志自动需要文件。例如:
<?php
# foo.php
function bar() { print "Bar.\n"; }
您可以像这样加载 PHP 解释器:
php -d auto_prepend_file=$PWD/foo.php -a
Session:
Interactive shell
php > bar();
Bar.
或者您可以手动包含文件:
php -a
Session:
Interactive shell
php > include 'foo.php';
php > bar();
Bar.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)