[development] module_exists vs. functions_exists?

Aaron Winborn winborn at advomatic.com
Mon Jun 23 11:49:33 UTC 2008

I usually use the following pattern:

if (module_exists('module_name')) {
  $foo = module_invoke('module_name', 'api_function', $bar);

----- Original Message -----
From: "Greg Knaddison - GVS" <Greg at GrowingVentureSolutions.com>
To: development at drupal.org
Sent: Sunday, June 22, 2008 5:36:46 PM GMT -05:00 US/Canada Eastern
Subject: [development] module_exists vs. functions_exists?


In building some contrib modules that interact with other modules I
often find myself writing something like:

if (module_exists('module_name')) {
  $foo = module_name_api_function($bar);

This seems to be pretty common across core and contrib.  I'd even call
it standard.

Another option would be:

if (function_exists('module_name_api_function')) {
  $foo = module_name_api_function($bar);

The benefit of the second format is that if/when module_name's
maintainer decides to rewrite module_name and change all their
functions then my module may simply stop interacting with it rather
than failing on a function not found error.

The benefit of the first format is that it fails quickly and is likely
to generate a bug report with a very clear solution.



Greg Knaddison
Denver, CO | http://knaddison.com | 303-800-5623
Growing Venture Solutions, LLC | http://growingventuresolutions.com

More information about the development mailing list