0

In my custom module I want to define a constant which takes its value from a function provided by another module. When I call this function in my module I get a 'call to undefined function' error. Curiously, if I call the function from inside another function, I don't get an error and it works fine. It's as if all module functions get loaded at the same time and then executed, but anything that is not a function gets executed first before the functions become available.

// MY_MODULE.module

$var = FUNCTION_FROM_OTHER_MODULE();
define('MY_CONSTANT', $var);

// use MY_CONSTANT in the other hooks and functions of MY_MODULE

Why am I getting this error and how can I set my constant with this function? Hope this makes sense. thanks.

1 Answer 1

1

You can use the standard global keyword provided by PHP. This is better placed somewhere early in a page load. One that I can think of is hook_menu or hook_init.

Personally though I would strongly recommend to use the variable_get() function provided by Drupal.

1
  • The original question before edited was about globals. Constants are nothing special either, probably the same applies. I still insist on using variable_get() / variable_set().
    – Wtower
    Commented Feb 13, 2014 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.