I have a custom module where I've separated out certain functions into other files for better readability.

For example:

  • ftw.module
  • ftw.youtube.inc
  • ftw.tmdb.inc

What's the best way to include the two '.inc' files from my '.module' file?

module_load_include() seems like the obvious choice, but its documentation says:

Do not use this function in a global context since it requires Backdrop to be fully bootstrapped, use require_once BACKDROP_ROOT . '/path/file' instead.

What does 'in a global context' mean?

Does it mean at the top of my module file, outside of any functions? Or something else? Requiring Backdrop to be fully bootstrapped seems like that'd always be the case from within my module, right?

Comments

Yes, it does mean outside your functions. You can use module_load_include() within functions but not outside. The YouTube module gives an example:

<?php
/**
 * @file
 * Youtube field module adds a field for YouTube videos.
 */

require_once (dirname(__FILE__) . '/youtube.inc');

/**
 * Implements hook_menu().
 */
function youtube_menu() {
  $items['admin/config/media/youtube'] = array(
    'title' => 'YouTube settings',
    'description' => 'Configure sitewide settings for embedded YouTube video fields.',
    'page callback' => 'backdrop_get_form',
    'page arguments' => array('youtube_settings_form'),
    'access arguments' => array('administer youtube'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}
...

This way is slightly easier than BACKDROP_ROOT as it is relative to your main module file.

Additionally, "outside" means a file that is located outside a module folder. The second parameter of module_load_include() is the name of a module, so, this function assumes that you are loading a file located within that module's folder. But if you wanted to load a file from (for example) an independent library that's not part of a module and is located somewhere in your installation, you need to use include_once or require_once.