我在 test.php 中有以下测试代码:
<?php
$step = $_GET['step'];
switch($step) {
case 1:
include 'foo.php'; # line 5
file_put_contents('foo.php', '<?php print "bar\\n"; ?>');
header('Location: test.php?step=2');
break;
case 2:
print "step 2:\n";
include 'foo.php';
break;
}
?>
foo.php 最初有以下内容:
<?php print "foo\n"; ?>
当我在浏览器中调用 test.php?step=1 时,我希望得到以下输出:
step 2:
bar
但是我得到了这个输出:
step 2:
foo
当我注释掉第 5 行中的 include 时,我得到了想要的结果。结论是,PHP 缓存了 foo.php 的内容。当我使用 step=2 重新加载页面时,我也得到了想要的结果。
现在...这是为什么以及如何避免这种情况?
最佳答案
假设您使用 OPcache,opcache.enable = 0
有效。
一个更有效的方法是使用
opcache_invalidate ( string $script [, boolean $force = FALSE ] )
这将从内存中删除脚本的缓存版本并强制 PHP 重新编译。
关于PHP缓存包含文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27786825/