Here is the code:

function module_load_include($type, $module, $name = NULL) {
  static $files = array();

  if (!isset($name)) {
    $name = $module;

  $key = $type . ':' . $module . ':' . $name;
  if (isset($files[$key])) {
    return $files[$key];

  if (function_exists('drupal_get_path')) {
    $file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$name.$type";
    if (is_file($file)) {
      require_once $file;
      $files[$key] = $file;
      return $file;
    else {
      $files[$key] = FALSE;
  return FALSE;

module_load_include() is just a function that calls require_once from within itself to load an external file. But it seems to me that require_once should load external file into module_load_include() and subsequently, this loaded file should disappear when module_load_include() goes out of scope. Somehow "magically" the external file is successfully loaded into the calling script(eg: a page callback function) that calls module_load_include(). I don't understand how this is achieved.

  • When a file is included, it stays included for the rest of the current PHP execution. This isn't a scope thing, nor even a Drupal thing. It's just how PHP works. – Jaypan Apr 5 '17 at 15:05
  • That's what I thought but my private experiment showed otherwise. I defined a function load_file($file){if(is_file($file)){require_once $file}} and inside $file I defined $var1 = 'var1 value'; But I couldn't read $var1 from the main PHP script. I could only read $var1 from within load_file() – okey_on Apr 5 '17 at 15:23
  • I can't speak as to your test - it may have been how you structured the code inside the file. But as I said, once a file is included, it remains included for the rest of the current process. – Jaypan Apr 5 '17 at 15:24

It sounds like you're getting confused on scope. There's no "file scope" in PHP. The include/require statements are pretty much the equivalent of dropping the code within the scope that it's called.

Per PHP's docs on the include statement:

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

If definitions in the included file are global. For example:

  • global $base_url;
  • define('MYMODULE_CONST', 123);
  • function mymodule_function() { }

Then they are global until the script execution ends.

If definition is not global (e.g. $var1 = 'var1 value';) then it only remains within the scope of the include statement.

  • I get your points and they are exactly what I believe happens in PHP. My question is: How was require_once able to include it's target file into a global/main script where said file is used, even though require_once was called from within the function module_load_include() which in turn is called from the main script? – okey_on Apr 7 '17 at 12:05
  • It depends on your specific example, but it depends on when the module_load_include() was called. For example, if it was called the at the beginning .module file, in a hook_boot() invocation, etc. If it was called early enough, it could be available at all points execution in this "main script" you're referring to. – Shawn Conn Apr 7 '17 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.