Jump to content

Variable name conflicts from includes. More complex var names or use unset?


maexus

Recommended Posts

In your includes, do you generally have longer more complex variable names or use unset for variables that aren't needed outside the include to prevent conflict of variables in the script calling the include?

 

In case that didn't make sense... consider the following

 

 

somefile.inc.php:

<?php
$paths = array('dir', 'dir2', 'dir3');
$base = dirname(__FILE__);
foreach($paths as $path){
$files = glob("{$base}/{$path}/*.ext");
foreach($files as $file){
	require_once($file);
}	
}
?>

 

index.php:

<?php
require_once("somefile.inc.php");
$paths = "another variable used for a completely different reason than the one used in the included file";
?>

 

Now, this will work, $paths will just be reassign but personally, this looks just sloppy. I see two options. Instead of using $paths, I could have $jfw_plugin_paths or use unset($paths) at the end of the include file. Which method do you use or do you just let the variable be reassigned when it's needed.

Use classes and functions and stop putting things in the global namespace and you avoid this problem.  This is why large chunks of procedural code become buggy and difficult to maintain.

Use classes and functions and stop putting things in the global namespace and you avoid this problem.  This is why large chunks of procedural code become buggy and difficult to maintain.

 

I smiled.

 

And I agree.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.