It seems that if require_once
is called within function, the included file doesn't extend the global variable scope. How to require_once
a file to global scope from within a function?
What I'm trying to do is some dynamic module loader:
function projects_init()
{
...
foreach ($projects as $p) {
require_once($p['PHPFile']);
$init_func = $p['init'];
if ($init_func)
$init_func();
}
}
If it is not possible to use require_once
that way, what is the simplest solution for this? (Please no heavy frameworks.)
EDIT: it should also work for PHP 5.2.
To summarize all the information:
functions are not an issue, they will be global anyway this way
for global variables, there are 2 options:
projects_init()
in my case)Functions are not an issue (ref):
All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.
About global variables: As in an existing question regarding the scope of require
and the like, the scope is defined where the use is. If you need something else, there are numerous answers (my take) that show how to deal with global variables, most making use of get_defined_vars
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With