Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP caching include file

Tags:

php

caching

I have the following test code in 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 initially has the following content:

<?php print "foo\n"; ?>

When I call test.php?step=1 in my browser I would expect the following output:

step 2:
bar

But I get this output:

step 2:
foo

When I comment out the include in line 5, I get the desired result. The conclusion is, that PHP caches the content of foo.php. When I reload the page with step=2 I also get the desired result.

Now... why is this and how to avoid this?

like image 906
Peter Avatar asked Dec 19 '22 08:12

Peter


1 Answers

Assuming you use OPcache, opcache.enable = 0 works.

A more efficient method is to use

opcache_invalidate ( string $script [, boolean $force = FALSE ] )

This will remove the cached version of the script from memory and force PHP to recompile.

like image 184
Philip Jones Avatar answered Dec 24 '22 01:12

Philip Jones