Hi,

I have developed a basic custom node module. One issue bugs me. I would like to find out .....is there a way to declare/create global arrays, variables, constants for use throughout a particular custom module ??....so that the arrays, variables or constants can be called/referenced directly from different functions in the module....

for example, i created this function to create an array..

<?php
function _AcademicStaff_Salutations() {
  return array(
'Professor','Dr','Mr','Ms');
}
?>

whenever I need to use this array I reference as follows :-

$salutations = _AcademicStaff_Salutations() ;

is there a way to set $salutations once and use it directly after in a module ?

I've come accross the use of variable_get, variable_set functions and the variables table...but I'm not clear if this is the way..

I would appreciate any feedback on this . Thanks alot.

Comments

dhar’s picture

Is there a particular hook that is normally used to set global parameters/arrays ??? I see reference is made to hook_init with some reservations...

modul’s picture

I'm not sure if I quite understand what you're after, but I think you could put your AcademicStaff() function in settings.php. Functions you declare there, are available throughout Drupal. But as I said, I'm not sure if that is what you want.

Ludo

dhar’s picture

I apologize if I was not clear initially..here goes..

I've seen that you could use the define() function to define a constant for use throughout a module. eg.

<?php
   define
(myname, "dhar jones");
?>

this is set at the beginning of the module....and not in any function in the module. Is it possible to do this for an array ...define it outside any function in the module. for example, define

<?php
$salutations
= ("Professor", "Dr", "Mr", "Ms");
?>

then, use $salutations as is in any function in the module.

dhar’s picture

Does anyone have any info on this...would really like to hear from you please... I'm learning to code in PHP/Drupal. I developing my first module ...which is why I'm asking..

nevets’s picture

I think the function approach is fine and follows the approach Drupal tends to use. Given your example,

<?php
function _AcademicStaff_Salutations() {
  return array(
'Professor','Dr','Mr','Ms');
}
?>

where the array is simply and static I think the code is fine as is. If you need to build the array I would code it something like

<?php
function _AcademicStaff_Salutations() {
  static
$salutations;
 
  if (empty(
$salutations)) {
   
// Build up the list of salutations, here we just use a static array
    // but this could be built up from database calls
   
$salutations =  array('Professor','Dr','Mr','Ms');
  }
  return
$salutations;
}
?>
dhar’s picture

I appreciate your assistance