[development] Convention for accessing global variables

Brian Vuyk brian at brianvuyk.com
Wed Dec 9 20:45:02 UTC 2009


Obviously, there are two ways to access global variables.

1. Via the global keyword:

<?php
  global $user;
  print $user->uid;
?>

2. By accessing the $GLOBALS array directly:

  <?php
    print $GLOBALS['user']->uid;
  ?>

Is there an established convention for this within Drupal core? The 
majority of cases in HEAD use the global keyword (~470) instead of 
accessing the $GLOBALS array (~40 cases). Is this something that should 
be made a coding style issue for consistency across the codebase?

As a sidenote, accessing the $GLOBALS array directly in on average 25% 
faster than using the global keyword. Of course, both run fast  enough 
that the difference is hardly noticeable.

Brian Vuyk



More information about the development mailing list