Jump to content

Recommended Posts

ok so here is the story thus far.

 

i made a wrapper that auto-includes php files within a given dir,

 

i use that dir strictly for php scripts that use the "define()" funtion.

i have about 5 or 6 of php pages in that dir and they load fine. how ever it is getting harder and harder to keep track of what i have already defined. so to help me out i wrote this script

 


SESSION_START();

$_SESSION['def']=scandir("common/defines");
$_SESSION['m']++;
if($_SESSION['def'][$_SESSION['m']])
{
 if($_SESSION['def'][$_SESSION['m']]!="." && $_SESSION['def'][$_SESSION['m']]!="..")
 {
  include("common/defines/".$_SESSION['def'][$_SESSION['m']]);
  $temp=get_defined_constants('user');
  $_SESSION['useddef'][]=$temp['user'];
 }
 header("Location: defcheck.php"); 
}
else
{
for($i=0; $i<=count($_SESSION['useddef'])-1; $i++)
{
	foreach($_SESSION['useddef'][$i] as $key=>$value)
	{
 	 	for($m=0; $m<=count($_SESSION['useddef'])-1; $m++)
		{
		 if($i!=$m && array_key_exists($key,$_SESSION['useddef'][$m]))
		 {
		  $used[$key]++;
		  $where[$key][]=$_SESSION['def'][$i+2];
		 }
		}
	}
}

if($used)
{
	foreach($used as $key => $value)
	{
	 if($value>=2)
	 {
	  $error .="Define '".$key."' is duplicated"."\n";
	  foreach($where[$key] as $location)
	  {	
	   $error .="	->".$location."\n";
	  }
	 }
	}
}
else
{
 $error .="All Defines where original";
}

unset($_SESSION['def']);
unset($_SESSION['m']);
unset($_SESSION['useddef']);
echo "<pre>".$error."</pre>";
}

 

if i define something twice on two differn't php scripts this works fine.

 

my problem is what if i define somthing twice on the same page?? does any one have an idea of how to check for that?

 

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/153971-solved-double-define/
Share on other sites

your right i should switch it to include_once();

however i want the script to look for this:

 

define("TEST","THIS IS A TEST",TRUE);

....

define("TEST","THIS IS ANOTHER TEST IN THE SAME FILE, I FORGOT I ALREADY USED TEST AS A DEFINE",TRUE);

 

i would like the script to display this as a resault.

 

once again at the moment it will do that provided they are in two differn't files

Link to comment
https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809272
Share on other sites

If your application has grown to the point where you are having problems with reusing defined constant names, you are at the point where you need to switch to modularized code using classes so that different functional sections of code do not interfere with each other.

Link to comment
https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809298
Share on other sites

well i dont realy know how to to use classes yet, so that will be on the list of stuff i should look up and study. by the sounds of it it could help me out alot.

 

as for the problem i wrote about.

 

i was thinking about useing defined(); the only prolem is i would have to figure out a wat to get it to run after each line of the include_once() function,

 

is their anyway to do that?

Link to comment
https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809307
Share on other sites

The assumption I had is that you would create a wrapper function that would take the name and value as a parameter.  The wrapper function would check defined() first and only do the define if it was no already defined. 

 

Don't get me wrong, I don't think this by any means necessary or a good idea, but it will solve your problem.  Your function should probably be called cDefine or myDefine or something like that.

 

Personally, my advice would be to keep all your defines in one include named constant.php or something like that.  PHP constants have several gotchas that make them rather inflexible, so I tend not to use them all that much.

Link to comment
https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809313
Share on other sites

no,

 

i have a wrapper for my website.

 

this wrapper looks in a dir called "common/defines", which is exclusivly for defines.

 

to keep my self organzied i diveded all my defines into 6 sections.

 

what i wrote runs on its own out side the wrapper, i plan on usinging it as a debugging tool.

 

it checks all the files in that dir to make use i didn't make two deinfes havethe same key.

the problem im running into is that it wont check for that mistake within the same script, only compaire scripts

Link to comment
https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809330
Share on other sites

Yes, what I'm saying to you is that you should create a function and put it in a file that you require_once() at the top of each of your scripts in the define directory.  That function could be called cdefine(), but otherwise will have the same parameters as define.  Then you simply search & replace all the calls to define() with cdefine(). 

 

The code could be as simple as:

 

function cdefine($key, $value) {
  if (!defined($key))
    define($key, $value);
}

 

Another option is to utilize the fact that define() will actually return true or false, so you can use that in an if statement or even trigger_error().

 

 

So you could just go ahead and modify all your defines to use something like this instead:

define('const', 'value') || trigger_error('constant already defined!', E_USER_NOTICE);

 

Link to comment
https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809340
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.