The correct way to include files in your Drupal module - Learning from the Drupal API

Most PHP developers are initiated into the world of includes with the following:

include($file);

or

require($file);

The difference between include and require is subtle. Require - as the function name indicates - requires that the included file exist to continue the script. So, if require fails, the script stops. Using include will allow the script to continue. Most of the time, using require makes more sense because it's likely that the file we want to include includes some important information for the rest of the script.

After a while, most of us discover that we might be inadvertently including the same file several times, and learn some new functions to insure our files aren't being loaded multiple times, namely include_once and require_once to prevent

That's better, but when we start developing in Drupal, we're likely going to be stringing this along with some other functions to get the correct path to the file we need to include. There's a nice little function available for us in the Drupal API which simplifies this code. Here's an example from content.module in the CCK module of module_load_include:

module_load_include('inc', 'content', 'includes/content.node_form');

Let's take a quick look at the code behind the scenes:

function module_load_include($type, $module, $name = NULL) {
  if (empty($name)) {
    $name = $module;
  }

  $file = './'. drupal_get_path('module', $module) ."/$name.$type";

  if (is_file($file)) {
    require_once $file;
  }
  else {
    return FALSE;
  }
}

First, the function assumes that we'll be using an include file that has the same name as the module. If not, no problem, we just set the name explicitly in the third parameter.

Next, a path to the file is generated based on the module name we pass to the function. If you're still wrapping your mind around how to create portable paths in Drupal, this is a nice example. The dot-slash (./) indicates that the file should only be looked for in the current directory, and not in any shared PHP library directories. The drupal_get_path function is a nice API call to generate a path to a module or theme directory. Note that you have to supply the code with the extension for the included file, which will often be 'inc.'

Finally, the function checks to see if the file exists using is_file(). There is another function that operates similarly, called file_exists(), and the difference between the two is that file_exists will return TRUE if a file OR a directory exists with the name. If you know you're looking for a file, is_file usually makes more sense.

There we go, a nice little function to save you some code repetition that also gives you some ideas for best practices on including files.

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.