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.
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 wantrequire __DIR__ . '/config.php';
, your current working directory will probably be the Drupal rootrequire __DIR__ . '/config.php';
, same result. I can usefile_get_contents( __DIR__ . '/config.php');
to show the content, but I can not access the variables.