Nifty function : module_load_include()

So I ran across this nifty function, and I was once again blown away by how little things like this in the Drupal API can really make your life easier… if you know about them

Disclaimer - yes, this has been available since D6… I’m late to the party…

Ok, so what this little gem does is simply load a modules include file in php. Simple. So why is this so cool? Because - you don’t have to know where the file is located in the system to make it work! And if the file moves later, you are still golden.

Use cases:

So when might you use it? Well, for starters you might need to load up something like node.pages.inc or something if you are early in the stack OR on a PHP page that doesn’t have the full system loaded. You may also want to check if a function is loaded, and then load the necessary include IF its not.

This is a fun idea - what about loading a different include file if the user is anon or auth… or an admin… or a content creator/editor/etc. A simple IF statement or switch can let you load up completely different batches of code… without having to load them all. This would be especially handy if you wanted to have multiple versions of the same function.

Now, I am sure that some of these ideas are way less than ideal… and perhaps a bit too crazy for practical use. Still, this function does deserve a place in your DAK (drupal army knife)… and the fact that its still around in D8 means that I’m not the only one who thinks that.