tags:

views:

114

answers:

5

In PHP when using the include function is the a way to tell from the inserted file which file inserted you? For example, I use the following line often through my code:

include 'header.php';

Is there a way to tell from inside header.php what PHP file inserted you?

+1  A: 

You could set a variable for this, but it's not a perfect solution...

$CallingFile = 'myfile.php';
include 'header.php';

header.php can now interrogate the variable $CallingFile to know who called it.

Sohnee
+2  A: 

While there's nothing builtin to the language, you could set up a coding pattern where a variable is set that tells you the source file doing the including:

$foo_php_old_includer = $includer;
$includer='foo.php';

include 'header.php'; // uses $includer to discern who is including it

// rest of source file

$includer=$foo_php_old_includer;

If every file had something like the above in it, you would create an "include stack" where each file would know which file included it.

All this being said I suspect the problem you are trying to solve might be better solved with a different methodology. If you could describe a bit the problem you are trying to solve with this method SO might be able to help you come up with a better solution.

fbrereto
+4  A: 
Paul Dixon
+1  A: 

The PHP manual lists the get_included_files function, which is sort of related to what you want... but one of the comments on that page says:

If you want to know which is the script that is including current script you can use $_SERVER['SCRIPT_FILENAME'] or any other similar server global.

Mark Rushakoff
+4  A: 

debug_backtrace can tell you that.

Gumbo