auto_prepend_file is not working in cli mode

 ̄綄美尐妖づ 提交于 2019-12-11 07:17:43

问题


I cannot get an auto_prepend_file run on my mac:

# cat /opt/local/lib/php/test.php
<?php
function test() { return 'foo'; }

# php --ini | grep php.ini
Configuration File (php.ini) Path: /opt/local/etc/php5
Loaded Configuration File:         /opt/local/etc/php5/php.ini

# cat /opt/local/etc/php5/php.ini | grep auto_prepend_file
auto_prepend_file = "/opt/local/lib/php/test.php"

# ls -la /opt/local/etc/php5/php.ini
-rw-r--r--   1 root  admin  68630 Jul 27 13:53 /opt/local/etc/php5/php.ini

# php -r "echo ini_get('auto_prepend_file');"
/opt/local/lib/php/test.php

But then...

# php -r "echo test();"
Fatal error: Call to undefined function test() in Command line code on line 1

# php -i | grep auto_prepend_file
auto_prepend_file => /opt/local/lib/php/test.php => /opt/local/lib/php/test.php

It works through the (restarted) apache.

Do you have any idea what I could have made wrong?


回答1:


auto_prepend_file is not executed when you run code directly through the -r commandline switch. It works fine when you execute PHP files on the commandline, regardless if PHP itself is called from the CLI or it is specified in the shebang of the executable file.




回答2:


This is documented behavior:

This option is only intended for very basic code, so some configuration directives (such as auto_prepend_file and auto_append_file) are ignored in this mode.

http://www.php.net/manual/en/features.commandline.options.php




回答3:


A workaround is to pipe the file content you want to execute to php instead of using -r. E.g.:

echo "<?php dump('anuise');" | php


来源:https://stackoverflow.com/questions/6844010/auto-prepend-file-is-not-working-in-cli-mode

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