0

I have this module's inc file, which hold page callback functions. Also I have a 'config.php' file that hold the parameters. I know this is not a good practice, but I would like to leave these config there for now.

Here is the question, I cannot get access to the parameters in the config.php file after I used require 'config.php' or require_once 'config.php'.

e.g. I have admin.inc which holds these:

<?php
    require 'config.php';
    
    function my_page_callback(){
        global $config;

        var_dump($config); //this prints NULL when I access this menu item.
    }

    

and config.php holds these an located in the same folder:

<?php

    $config = array();
    $config['key'] = 'value';
    $config['name'] = 'foobar';

The file is successfully included since there is no fatal error, but why can't I get these values?

update

var_dump($config); this would print the right content after I move the require statement to the body of my_page_callback, but I don't think this is how PHP works.

Explicitly set $config as global in the required file could give non-NULL result, but it is not needed in my test files.

21
  • That doesn't add up - a failed require will result in a fatal error, so printing NULL could never happen. Unless you mean you see a white screen? And yes, with the best will in the world, that's a shocking way to store your config variables given that Drupal has entire systems dedicated to it. You're even forcing yourself to use global vars. Highly recommend doing it the proper way, it'll save you headaches in the future. By the way, to fix, you probably just want require __DIR__ . '/config.php';, your current working directory will probably be the Drupal root
    – Clive
    Jul 14, 2015 at 7:59
  • @Clive I am new to module development, so it will take some time to prepare a proper page used to set these variables which will never be changed, so I choose this quick and dirty way. But I still don't understand why this happen. It doesn't make sense to me.
    – leetom
    Jul 14, 2015 at 8:25
  • If they never change surely they can be defined as constants at the top of the module rather than including them in a separate file Jul 14, 2015 at 8:30
  • I have tried require __DIR__ . '/config.php'; , same result. I can use file_get_contents( __DIR__ . '/config.php'); to show the content, but I can not access the variables.
    – leetom
    Jul 14, 2015 at 8:34
  • In that case I'd say it's because you're missing a semi-colon at the end of the statement.
    – Clive
    Jul 14, 2015 at 8:37

3 Answers 3

1

I'd comment on sceo's answer, but I can't yet.

More information on declaring it as global $var: https://www.drupal.org/node/633230#comment-2267370

But as is recommended here, in the link and everywhere really, if you want globals use variable_set and variable_get (or use constants? You can serialize the array, as constants before PHP 5.6 can't accept arrays).

You can also make a function in your module file that simply returns the array you want to use. Not sure how correct or efficient that is, but it does work.

function mymodule_get_config() {
    return array(
      'key' => 'value'
    );
}
1
  • Thanks for the information provided in the link, this answered my question! hmm, already answered 6 years ago :)
    – leetom
    Jul 15, 2015 at 14:14
1

You can use this approach to include a file in .module

function module_name_init(){
    module_load_include('inc', 'module_name', 'includes/common');
}

and place common.inc file in includes folder in your custom module.Now write your php code here.

1
  • Thank you, this is a good solution. But I am still curious about the strange behaviour here.
    – leetom
    Jul 15, 2015 at 14:09
0

Agree that this is bad practice and you should be setting these values using variable_set and variable_get, or with a module config form.

However, it should work the way you have it with the exception that you will need to 'global $config' in the config.php file AND then again in my_page_callback().

You would do better to rename $config to something more specific, so you don't have collisions with anything else in the global scope, so change $config in config.php to something like $my_module_config or something.

1
  • I have some prefix in the variable names, those lines are just mockup of hundreds lines of codes. Could you provide a link that says need to 'global $config' in the config.php file AND then again in my_page_callback().? I don't see any clue about this, and ` global $config = array()` will generate a syntax error.
    – leetom
    Jul 15, 2015 at 14:07

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.