View Full Version : Resolved Constants available in include file?

03-08-2010, 04:34 PM
I am including a file, left_nav.php, into my index.php file. I have a config.inc.php file that sets a bunch of constants for me to use throughout the site which is also included in index.php. It seems that the left_nav.php file doesn't pick up the constants, but seeing as they are included into the index.php I would expect their global-ness to apply them to the left_nav.php as well.

Now, I'm new to figuring out the scope stuff, but this seems to the the case elsewhere as I have an autoload function to pull in the appropriate classes as needed. One of those classes is a MYSQl class, and the parent constuctor uses the defined constants to initiate the MYSQL connection with no problem. Am I missing something???

03-08-2010, 06:48 PM
Are you ensuring that you're configuration file is included prior to you're left_nav.php file?
If you're index.php file includes you're config file, it will provide it via the scope of the call. Being constants, these are global anyway. So, when you then include the left_nav.php into the index.php after the configuration information, then yes it should be included at that point as well.
However, if there is ever an instance when a file can standalone, you may as well include anything and everything it needs at link time instead of depending on it being available at runtime. I'm a huge fan of encapsulating everything dependant together whenever possible - in this situation, I'd be including the configuration file in left_nav.php as well. Just ensure that you're using an include_once/require_once call to ensure that the constants are not attempting a redefinition.

03-08-2010, 08:08 PM
The config file is brought in by include_once right after the session is started, before anything else happens. The left_nav is brought in half way down the page. I thought I was reading it right that the global scope would make it available to all files included after.

Should I be using include_once or require_once? I'm not sure the difference, even after reading up on it...

03-08-2010, 08:25 PM
Require will throw a fatal error if it cannot include the file, while include will silently fail. Require is nice since it will toss a fatal error without needing to add extra scripting when a failure occurs. Include on the other hand will silently fail and let you continue. Once will only include the file once, so things like constants and functions will not try to redeclare themselves (which will result in a fatal error). I'd use a require when it comes to configurations unless you have a fallback for them.

Add this to you're left_nav.php file to see if you're constants are declared:

$getConstants = get_defined_constants(true);
$userConstants = &$getConstants['user'];

Do all of you're constants appear there?
Also, one thing that should be noted: these are different:

// And

The latter is a global constant. The first is a namespace constant, and is not called with a standard constant replacement call. Ensure you're defining them with define('MYCONST', 'CONSTANT VALUE');.

03-08-2010, 08:33 PM
I'm using define() to set the constants. Interestingly, when I added your constant check code to the left_nav, they magically started working and continued to do so when I removed it... Very odd, but thanks for fixing it ;)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum