Module dependent on extension I am developing a small module which is dependent on the php_ldap extension. I would like to make sure that this extension is enabled before activating the module and if it is not display a message telling the user that the ldap extension needs to be installed and enabled. I have tried using hook_enabled() for this purpose, but since this hook is fired after the module is enabled it will not work as expected:

function example_enable() {
  if (!function_exists('ldap_connect')) {
    // Here I would like to halt the activation of the module
    drupal_set_message(t('The Ldap extension needs to be enabled.'), 'error');
  }
}


I am not sure how to stop the user from enabling a module if a specific extension is not installed or loaded. I was thinking about using hook_install(), but I am not sure that would work either.
The best solution for you is using hook_requirements .

function hook_requirements($phase){
       if ($phase =="install"){
         $requirements['php'] = array(
           'title' =&gt; 'LDAP extension',
           'value' =&gt; "LDAP",
          );
 if (function_exists('ldap_connect')) {
      $requirements['php']['description'] = 'YOU NEED TO install LADAP';
      $requirements['php']['severity'] = REQUIREMENT_ERROR;
     }
    }
return $requirements;

}


and another tricky solution (but NOT RECOMMENDED) is : Create Another module for php_ldap extension (let name it B), and on that module on hook_enable check the php_ldap extension is exists if not,  uninstall module(uninstall B module) , and  set your main module  dependent on first one(I mean Module B) . with this method you will be sure if the extension Available Module B can be installed and consequence Your main Module. 

Something like  following snippet could help you to implement that

function MODULEB_enable() {
  if (!function_exists('ldap_connect')) {
        module_disable(array('MODULEB'));
       drupal_uninstall_modules(array('MODULEB'));
       drupal_set_message(t('The Ldap extension needs to be enabled.'), 'error');
  }
}