M.O.S. Studios Posted April 14, 2009 Share Posted April 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153971-solved-double-define/ Share on other sites More sharing options...
kenrbnsn Posted April 14, 2009 Share Posted April 14, 2009 Use include_once instead of include. Ken Quote Link to comment https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809267 Share on other sites More sharing options...
M.O.S. Studios Posted April 14, 2009 Author Share Posted April 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809272 Share on other sites More sharing options...
gizmola Posted April 14, 2009 Share Posted April 14, 2009 You can check for a constant definition using the defined() function. Quote Link to comment https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809278 Share on other sites More sharing options...
PFMaBiSmAd Posted April 14, 2009 Share Posted April 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809298 Share on other sites More sharing options...
M.O.S. Studios Posted April 14, 2009 Author Share Posted April 14, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809307 Share on other sites More sharing options...
gizmola Posted April 14, 2009 Share Posted April 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809313 Share on other sites More sharing options...
M.O.S. Studios Posted April 14, 2009 Author Share Posted April 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809330 Share on other sites More sharing options...
gizmola Posted April 14, 2009 Share Posted April 14, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809340 Share on other sites More sharing options...
M.O.S. Studios Posted April 14, 2009 Author Share Posted April 14, 2009 oh, that makes alot of scence :S, should have thoguht of that a while ago, thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/153971-solved-double-define/#findComment-809764 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.