Dynamically Loading Files Loses Variables

I created a function a while back as an experiment that works in either loading whichever files are included in $siteFiles as a comma-separated list in the path defined in $sitePath or excludes the files in the list.

The issue is, that while it is indeed loading the files which contain configuration as variables, the variables are not available on the site. Using the same include_once() manually does load them and works well so the issue is not in the included files but rather in the way they are loaded when doing to dynamically. What did I miss here?

// LOAD SECURE CONFIGURATION FILES
// include_once("{$SecurePath}config.php");
// include_once("{$SecurePath}keys.php");`

Calling the function

// LOAD SECURE CONFIGURATION FILES
loadPHPFiles($SecurePath, "config.php, keys.php", 1, 0);

The function itself

// INCLUDES FILES FROM LIST IN SPECIFIED DIRECTORY
// $sitePath		= PATH FROM WHICH TO LOAD FILES, MUST HAVE TRAILING SLASH
// $siteFiles		= EMPTY, FILE NAME, ARRAY OR COMMA-SEPARATED LIST OF FILES
// $includeFiles	= 1 TO INCLUDE THOSE LISTED; 0 TO EXCLUDE THOSE LISTED
// $Sort 0: NONE; 0: ASCENDING; 1: DESCENDING
function loadPHPFiles($sitePath, $siteFiles="", $includeFiles=0, $Sort=0) {
	$siteFiles	= ($siteFiles !== "" && !is_array($siteFiles)) ? array_map('trim',explode(',',$siteFiles)) : $siteFiles;
	$allFiles		= scandir($sitePath, $Sort);
	$allFiles		= array_diff($allFiles, array('.', '..', '.htaccess'));

	if (is_array($allFiles)) :
		foreach ($allFiles as $fileName) :
			// INCLUDE ONLY PHP FILES IN $siteFiles ARRAY
			if (pathinfo($fileName, PATHINFO_EXTENSION) !== "php") continue;
			// IGNORE ANY index.php FILE THAT MAY EXIST
			if ($fileName === "index.php") continue;
			// EXCLUDE BACKUP FILES AND PREVIOUS VERSIONS
			if (str_contains($fileName, 'BAK')) continue;
			if (str_contains($fileName, 'OLD')) continue;

			if (is_array($siteFiles) && $includeFiles == 1) :
				// INCLUDE FILES IN $siteFiles ARRAY; EXCLUDE ALL OTHERS
				if (!in_array($fileName,$siteFiles)) continue;
				$file = $sitePath.$fileName;
				include_once($file);
			elseif (is_array($siteFiles) && $includeFiles == 0) :
				// EXCLUDE FILES IN $siteFiles ARRAY; INCLUDE ALL OTHERS
				if (in_array($fileName,$siteFiles)) continue;
				$file = $sitePath.$fileName;
				include_once($file);
			else :
				// INCLUDE ALL PHP FILES IN PATH (USE WITH CAUTION)
				$file = $sitePath.$fileName;
				include_once($file);
			endif;

			 // PAUSE BRIEFLY FOR EVERY LOOP
			// sleep(2);
		endforeach;
	endif;
}

Because the include (you should use require for things your code must have) is executed inside of a function, any variables defined in the file(s) are local to that function.

Hmm, You’re right, I hadn’t thought of that as I expected the files to still be the files. It never occurred to me that they would become the scope of the function! That explains why files containing only other functions still work while those containing variables do not!.

If you really want to do this, you can - return get_defined_vars(); inside the function, then use extract() on the returned value to populate the global variable space with variables defined in the files.

That’s a thought. I’ve never used either of those before so will have to look into how it’s done but seems it’s what I need.

Sponsor our Newsletter | Privacy Policy | Terms of Service