title: How can I run a php script exactly once - No sessions
body: <p>I have the following question: how can I run a php script only once? Before people start to reply that this is indeed a similar or duplicate question, please continue reading...<br /></p>
  
  <p>The situation is as follows, I'm currently writing my own MVC Framework and I've come up with a module based system so I can easily add new functionality to my framework. In order to do so, I created a <code>/ROOT/modules</code> directory in which one could add the new modules.</p>
  
  <p>So as you can imagine, the script needs to read the directory, read all the php files, parse them and then is able to execute the new functionality, however it has to do this for all the webbrowsers requests. This would make this task about O(nAmountOfRequests * nAmountOfModules) which is rather big on websites with a large amount of user requests every second.</p>
  
  <p>Then I figured, what if I would introduce a session variable like: <code>$_SESSION['modulesLoaded']</code> and then simply check if its set or not. This would reduce the load to O(nUniqueAmountOfRequests * nAmountOfModules) but this is still a large Big O if the only thing I want to do is read the directory once.</p>
  
  <p>What I have now is the following:</p>
  
  <pre><code>/** Load the modules */
  require_once(ROOT . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'module_bootloader.php');
  </code></pre>
  
  <p>Which exists of the following code:</p>
  
  <pre><code>&lt;?php
  //TODO: Make sure that the foreach only executes once for all the requests instead of every request.
  if (!array_key_exists('modulesLoaded', $_SESSION)) {
      foreach (glob('*.php') as $module) {
          require_once($module);
      }
      $_SESSION['modulesLoaded'] = '1';
  }
  </code></pre>
  
  <p>So now the question, is there a solution, like a superglobal variable, that I can access and exists for all requests, so instead of the previous Big Os, I can make a Big O thats only exists of nAmountOfModules? Or is there another way of easily reading the module files only once?</p>
  
  <p>Something like:</p>
  
  <pre><code>if(isFirstRequest){
      foreach (glob('*.php') as $module) {
          require_once($module);
      }
  }
  </code></pre>