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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.