Run PHP function from parent file
I need some help for a problem i'm facing. I've look on the web but i only found more usual issues which cannot solve mine.
The main idea is to run PHP functions likeget_included_files()
,get_defined_vars()
,debug_backtrace()
from the parent file.
In my code i have a PHP file, let's call it A.php which is including another PHP file, let's say B.php.
I would like to run for exempleget_included_files()
into B.php to get the files included in A.php.
Something similar to parent:: but for PHP files and not oriented object programming. I'm aware that it could be a security flaw, that's why i'm not sure it is possible, but i ask to be sure.
To be the clearest possible, here is an example :
A.php
include 'B.php';
include 'C.php';
include 'D.php';
get_includes();
B.php
function get_includes()
{
$included_files = get_included_files();
print_r($included_files);
}
And i need the$included_files
var to containarray('B','C','D')
.
My problem is the PHP functions getting the included files from the file it's launched in, not from the parent (with this code, the$included_files
is empty).
Answer
Solution:
php is an interpreter, and interpretation will be strings. So the file a.php try to include files in the following order:
Answer
Solution:
Consider the example:
a.php
b.php
Answer
Solution:
I don't know if it would solve your problem, but you can also execute these function in the parent file, assign the result to a variable, and read this variable in included files.
For instance:
A.php
B.php
C.php
It will give an output that looks like this:
Note that you can't get info about files and vars that are included/defined after the call to the function, so anyway from B.php you can't know that C.php and D.php will be included later.