Hi
I want to create a config file that declare some constant variables for using in my custom module. I donot want to use variable_get() as I donot want to use database for this case

Could you please tell me how to do that

In normal php, I can declare a constant in a file then use "include ('blaba')" ; but I do not know how to do that in drupal.
Could you please help
john

Comments

andyf’s picture

Use define at the beginning of your .module?

dman’s picture

I'm guessing you mean globals, not constants?
There is nothing special about using constants define('MY_CONSTANT', 'set normally') in Drupal.

Globals are generally considered harmful, which is why you won't see a lot of them.
But if you want, and you prefix the name with your module name, it's your choice.

There IS something special about global handling if you come from the old raw-php way of building includes. Thanks to PHP scoping rules, globals have to explicitly be declared global even if they were in file-level scope.
so

// mymodule.module
$myglobal = "is set";

function my_callback() {
  global $myglobal;
  return $myglobal;
}

will not work

Because the module file was included by a handler deeper in the system, earlier, and that file-level assignment was forgotten almost immediately.

So if you want global vars, declare it so.

// mymodule.module
global $myglobal;
$myglobal = "is set";

function my_callback() {
  global $myglobal;
  return $myglobal;
}

Will work.

In short, don't trust file-level variable assignments.

Also variable_get() is heavily cached automatically, so it's always handy, and not something to avoid unless it's huge lumps of data.

jaypan’s picture

variable_set() and variable_get() were made for this precise situation. Not using them makes no sense.

Contact me to contract me for D7 -> D10/11 migrations.

johnhelen’s picture

Thanks,

I want to use variable_get() and variable_set(). However that constants should be accessible from a client php application, not only from Drupal site. That is why I need to declare these variables in somewhere so that both Drupal (admin) site and client application can use them

ok, I should use define (....) in my module