Drupal coder: you don't have to stick to the parameter names in the documentation

Submitted by Frederic Marand on

The documentation for hook_user can cause an unforeseen problem...

The API documentation for hook_user names the four parameters for hook_user as:

  • $op
  • &$edit
  • &$user
  • $category = NULL

which is all very well, except for the fact the $user happens to also be the name of a global variable in Drupal. So unsuspecting developers will notice changing behaviour for some cases if they use the global $user: for nodes being edited by someone not already the node author, the author information can be lost.

The fix is exceedingly simple: just rename the param to another name of your choosing, like $account, suggested by Moshe and Drumm as it appears to be already renamed that way in some places within Drupal.

Note that the same is true for various places in Drupal. Unlike, say, Delphi Pascal, PHP does not rely on function definitions matching prototypes down to the parameter name: Drupal doesn't even use prototypes, as it dynamically builds the hook names and invokes them from the dynamically built variable. So don't hesitate if you don't like a param name !

Tagged for , , .

Hi, Yvan,

Indeed I did notice this node. But I refrained from quoting an offer because I think there are people in the drupal ecosystem like chx or killes who could do that in their sleep with more accurate results than me. There are still many things in core that puzzle me. And it seems sometimes they do because they're not (yet) properly done (did you look at the various field definitions for a module name throughout drupal ? three different sizes for the same field).

Cela dit, si Linagora a un besoin concret dans le domaine, je suis évidemment ouvert à toute proposition: en ce cas, merci de me contacter par courriel.