title: Caching includes in PHP for iterated reuse
body: <p>Is there a way to cache a PHP <code>include</code> effectively for reuse, without APC, et al?</p>
  
  <p>Simple (<em>albeit stupid</em>) example:</p>
  
  <pre><code>// rand.php
  return rand(0, 999);
  
  // index.php
  $file = 'rand.php';
  while($i++ &lt; 1000){
      echo include($file);
  }
  </code></pre>
  
  <p>Again, while ridiculous, this pair of scripts dumps 1000 random numbers. However, for every iteration, PHP has to hit the filesystem (<em>Correct? There is no inherit caching functionality I've missed, is there?</em>)</p>
  
  <p>Basically, how can I prevent the previous scenario from resulting in 1000 hits to the filesystem?</p>
  
  <p>The only consideration I've come to so far is a goofy one, and it may not prove effective at all (<em>haven't tested, wrote it here, error prone, but you get the idea</em>):</p>
  
  <pre><code>// rand.php
  return rand(0, 999);
  
  // index.php
  $file = 'rand.php';
  $cache = array();
  while($i++ &lt; 1000){
      if(isset($cache[$file])){
          echo eval('?&gt;' . $cache[$file] . '&lt;?php;');
      }else{
          $cache[$file] = file_get_contents($file);
          echo include($file);
      }
  }
  </code></pre>
  
  <hr>
  
  <p><em>A more realistic and less silly example:</em></p>
  
  <p>When including files for view generation, given a view file is used a number of times in a given request (<em>a widget or something</em>) is there a realistic way to capture and re-evaluate the view script without a filesystem hit?</p>