background image
Eric's picture

The Drupal framework and theme engine are very powerful. Whenever I override a function, I start with the following code to see what variables are defined in the current function scope...

<?php
echo "<pre>";
print_r(get_defined_vars());
echo
"</pre>";
?>

General rule on variable definition

hi,

inshort: what is the rule on variable declaration and definition?

inLength:
i am trying to create a module for Drupal.

my php function works fine standalone. but when i call it from within drupal it does not.

after many hours of debugging, i found out i need help:)
- All global variables in my code are not accessible. what is the rule on variable declaration and definition?
- what do developers use for debugging? :)

thanks

Eric's picture

variables

Short answer: it depends. When I need to allow the user to choose settings for a module, I create an admin settings page. This allows you to use variable_get() in your module code to pull their settings.

Generally the only variables I define outside of Drupal hook functions are global constants [using define()]. To access them within your functions, you can just call them directly (see: http://php.net/manual/en/language.constants.php).

A lot of modules pull Drupal variables into function scope using the global keyword. For example:

<?php
function myfunction() {
  global
$user;
  echo
$user->uid;
}
?>