PHP defines valid function names as [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.

I have already been bitten by Drupal name conflicts when using hook_form_alter clashing with my module name when it contained common words separated by an underscore "_" ("isys_helper_form_alter").

So I thought about using the broken pipe character instead like I have done in other PHP projects ("¦", char #A6, "isys¦helper_form_alter").

Unfortunately, Drupal does not seem to find any functions I name using characters in the 7F to FF range.

Is this a documented difference with Drupal vs. PHP?

Searching the documentation online did not state any such difference, but I could have missed it.

  • Is isys_helper_form_alter() an implementation of hook_form_alter()?
    – apaderno
    Oct 24 '12 at 3:43
  • yes, for a module named "isys_helper". I was forced to change the module name to "isyshelper" before the function to be recognized properly. Oct 24 '12 at 14:38
  • That's an amazingly bad idea. Stick to A-Za-z0-9_ for function naming. Your colleagues will thank us both. Oct 31 '12 at 1:13

That's a bad idea. Please refer to Drupals Coding Standards under the section Naming Conventions.

In my opinion function/class/var names shouldn't contain anything else than A-Za-z0-9_.

If you don't want isys_helper_form_alter to be picked up by Drupal, you can always prefix it with an underscore: _isys_helper_form_alter.


Here's another topic on why other characters are bad: https://stackoverflow.com/questions/6362241/php-variable-function-class-names-using-special-characters. And even if you remember to save it in the right encoding, other people who needs to maintain your code later on might not do that.

  • I DO want Drupal to pick up isys_helper_form_alter, but it isn't being picked up. I have to change the module name to isyshelper_form_alter in order for it to work correctly. Oct 24 '12 at 14:22
  • Official docs stated that underscores in module names are "ok", but several Drupal developers besides me have run into name collision issues with having underscores in module names such that many strongly say module names shouldn't contain underscores. Despite the potential pitfalls, I would still say matching what PHP allows is better than not doing so (or specifying what chars instead of the generic "lowercase" or "letters"). But that's just my opinion. Oct 24 '12 at 14:45
  • Using underscore in module and function names should not be a problem. Even core does it!. Just make sure you don't get any collision with an existing module and a hook prefix. E.g. naming a module MODULE_NAME_hooksuffix when it also matches MODULENAME_HOOK_hooksuffix (can't really come up with a real example but I'm pretty sure they exist). Prefixing your module with the project name usually makes sure it's safe from collisions.
    – hampusn
    Oct 24 '12 at 20:51

Drupal allows you to use those characters in a function name. In fact the DRUPAL_PHP_FUNCTION_PATTERN constant is defined as follows.

define('DRUPAL_PHP_FUNCTION_PATTERN', '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*')

In your case, the problem is that the short name of the module would not match with initial part of the function name. If the short name of the module is isys_helper, then every hook implemented by that module must have a name that starts with isys_helper_. You cannot have a module called isys_helper for which the hook is called isys¦helper_form_alter().

The name collision happens when Drupal is not able to understand for which module the function is a hook implementation, and for which hook.
Suppose you create a module called user_node.module that defines a new content type, for which it implements hook_view(). The function implementing that hook for that module would be called user_node_view(), but that would be also the name for hook_node_view() implemented from the User module. This means that the following code would user_node_view() twice, provided that both user_node.module, and user.module are enabled.

// Call any implementation of hook_view().
$node = module_invoke_all('view', $node, $view_mode);

// Call any implementation of hook_node_view().
module_invoke_all('node_view', $node, $view_mode, $langcode);

Surely using underscores in the short name of a module doesn't help, but the solution is not to prefix the name of a hook with a string that is different from the one Drupal is expecting.

Solutions have been proposed to avoid issues, such as using two underscores to separate the module name from the hook name. In the example I made the implementation of hook_node_view() made from the User module would be called user__node_view() while the implementation of hook_view() for the other module would be user_node__view(). In this way, the ambiguity would be avoided.
Another proposal has been to use PHP 5.3 namespaces, but it is probable that even with Drupal 8 the hook names will follow the same schema used in other Drupal versions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.