I have designed a custom include function for my application, which was meant to make my life much easier except it's done the complete opposite and has resulted in me debugging the same type of errors for hours until this lightbulb moment realising what it was.
Because the files are being included into a function, the variables, functions, etc. inside the file are not global.
Is there any way that I can make it work, or is this whole exercise pointless?
See below for the code.
function import($a,$b = 1) {
global $options;
if($b == 1) $folder = "internal_functions";
elseif($b == 2) $folder = "external_functions";
else $folder = $b;
$file_path = $options['LIB_DIR']."/".$folder."/".$a.".php";
if(@!file_exists($file_path)) {
$_criticalFiles = array("sessionService/session_loader", "databaseService/db_connection","userService/isLoggedIn");
if(in_array($a,$_criticalFiles)) {
die("<center><h1>Critical Error.<br />Could not find {$a}.</h1></center>");
}
} else {
require_once($file_path);
}
}
Thanks
For those of you wondering how I got around this, I didn't.
Instead I put all the files in an array such as
$filesInclude = array("file1","file2","file3","etc");
2 different arrays for both local and external files, then just ran a for loop on the array. :)
It's a problem of scope. Any variables created in the function which you want available outside it will have to be global
ized.