I've never seen anything like this.
File structure:
- somefolder/base.php
- includes/stuff.php
- includes/constants.php
base.php:
<?php require("../includes/stuff.php"); ?>
stuff.php:
<?php
require("constants.php");
echo "Welcome to stuff.php";
?>
constants.php:
<?php echo "IMPORTANT CONSTANTS DEFINED HERE!"; ?>
Result:
Welcome to stuff.php
Why doesn't it display the constant.php message?! I'll tell you what isn't the problem: it isn't the php code.
I started off with some complex files where the constants file was actually DEFINE()-ing some constants and for some reason the only the first few constants were working. I ended up disabling all the extraneous code including the DEFINE()'s and just having echo statements and this problem still kept staring me in the face. And by the way how could some of the defined constants make it through but not any echo's, regardless of the order of the code?
So I finally realized this was absurd and decided to just create some new files as a test. Those ones worked. Or maybe they didn't, I forget. Anyway I have had some measure of success by creating new files and experimenting with moving them around and copying code from one file to another but there hardly seems to be any rhyme or reason to it.
Wow, this just in: I got the constants.php file working under a different name but when I changed it to constants.php it stopped working! I tried clearing the cache/opening in a different browser and restarting the server but none of it helps. I feel like either I or my computer is on drugs. Gaaaahh!!!
EDIT
Aha. I left out one very important piece of information it looks like. One file I didn't mention.
- somefolder/constants.php
That file was being included instead of the includes/constants.php. That's why I wasn't getting any missing file errors. That was totally confusing things! I expected that sub-includes happen before higher level includes so stuff.php would look for constants.php in it's own directory but I guess base.php includes stuff.php and THEN base.php does the include specified in stuff.php which means the include is relative to base.php. So absolute paths is the solution after all.
Additional edit:
Very interesting. I was still puzzled why it worked before when I simply changed the name of includes/constants.php (to constanza.php for example). It looks like file inclusion isn't so cut and dry. If there are two files with the same name it will prefer the one relative to the parent file doing the including. But if it has no choice it will do it relative to the child just fine.