php手册说:
http://php.net/manual/en/ini.core.php#ini.auto-prepend-file
Specifies the name of a file that is automatically parsed before the main file. The file is included as if it was called with the require function, so include_path is used.
让我们做一个测试
register_shutdown_function.php
<?php
error_reporting(-1);
ini_set('display_errors', 1);
register_shutdown_function('foo');
function foo() {
if(!is_null($e = error_get_last())) {
echo 'bar';
}
}
?>
.htaccess
php_value auto_prepend_file /www/public_html/register_shutdown_function.php
index.php
<?php
echo '
?>
结果:显示“栏”。可以正常工作。
下一个测试
我们删除.htaccess并更改index.php,如下所示:
<?php
require('register_shutdown_function.php');
echo '
?>
现在不调用register_shutdown_function()吗?
它仅适用于以下index.php:
<?php
require('register_shutdown_function.php');
include('file_with_parse_error.php');
?>
这是Bug还是与PHP 5.4.37(FastCGI)有关的东西?我问是因为有时我需要多次删除和/或覆盖.htaccess文件才能“启用”或“禁用” auto_prepend_file。
最佳答案
PHP必须先解析文件,然后才能运行它。
由于echo '
是一个解析错误,因此您的文件将完全不会运行,这意味着不会发生需求。
如果您的错误是echo 1/0;
(不是解析错误),则您的文件将按预期工作。
编辑:您需要在PHP读取具有解析错误的文件之前调用register_shutdown_function
。在第一个和第三个示例中,您正在这样做。
关于php - auto_prepend_file与 “require”一样吗? (使用register_shutdown_function失败),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28550482/