tibberous Posted April 13, 2009 Share Posted April 13, 2009 I am REALLY tired I have to type: $city = mysql_real_escape_string(trim($_REQUEST['city'])); $state = mysql_real_escape_string(trim($_REQUEST['state'])); ect... Its like 15 lines of copy and pasted crap everytime I have a form. Here is my idea - variable contexts. It would work like this: define context ":" ($x) { return mysql_real_escape_string(trim($x)); } Then, instead of saying: $city = mysql_real_escape_string(trim($_REQUEST['city'])); You could just use: $:city Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/ Share on other sites More sharing options...
premiso Posted April 13, 2009 Share Posted April 13, 2009 Why not, and this is just out on a limb, put the form input names in an array: $inputs = array('city', 'state', 'county'); Then do a loop through that array: foreach ($inputs as $key) { $_REQUEST[$key] = trim($_REQUEST[$key]); $$key = isset($_REQUEST[$key])?mysql_real_escape_string($_REQUEST[$key]):null; } However, I would just use the array version and not do a variables variable to define the value. But that is me. As far as variable context, if that is not incorperated I doubt it would/should be. As it is easily done with a loop as seen above without having to do the $:variable for each variable you want to use. Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808576 Share on other sites More sharing options...
Daniel0 Posted April 13, 2009 Share Posted April 13, 2009 Sounds like you just need to learn about proper application design. Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808585 Share on other sites More sharing options...
nrg_alpha Posted April 13, 2009 Share Posted April 13, 2009 tibberous, if your IDE supports macros, you could use that instead. I make use of plenty of macros (both default and custom code snippets) which saves time. Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808637 Share on other sites More sharing options...
Daniel0 Posted April 13, 2009 Share Posted April 13, 2009 tibberous, if your IDE supports macros, you could use that instead. I make use of plenty of macros (both default and custom code snippets) which saves time. Not if it means violating DRY. Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808642 Share on other sites More sharing options...
laffin Posted April 13, 2009 Share Posted April 13, 2009 gee and I thought that making yer own function was also good for as well... <?php funtion my_escape($string) { return mysql_real_escape_string(trim($string)); } Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808649 Share on other sites More sharing options...
nrg_alpha Posted April 13, 2009 Share Posted April 13, 2009 Not if it means violating DRY. I'm sorry... DRY? Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808708 Share on other sites More sharing options...
Mchl Posted April 13, 2009 Share Posted April 13, 2009 Do Repeat Yourself? Just guessing (oh... google says it actually stands for 'do not repeat yourself'... so why there is no N in there?) Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808714 Share on other sites More sharing options...
nrg_alpha Posted April 13, 2009 Share Posted April 13, 2009 Ah, I see. Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808717 Share on other sites More sharing options...
Maq Posted April 13, 2009 Share Posted April 13, 2009 Do Repeat Yourself? Just guessing (oh... google says it actually stands for 'do not repeat yourself'... so why there is no N in there?) No 'N' because you could say, "Don't". Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808719 Share on other sites More sharing options...
Mchl Posted April 13, 2009 Share Posted April 13, 2009 Ok... so DNR stands for 'Don't resuscitate' or for 'Don't not resuscitate' ? Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808739 Share on other sites More sharing options...
Maq Posted April 13, 2009 Share Posted April 13, 2009 Ok... so DNR stands for 'Don't resuscitate' or for 'Don't not resuscitate' ? Maybe...? Do you think if it stands for "Don't" then it should be an exception for "do not" and vice versa? Sometimes acronyms don't follow the rule, like PHP for example, "Hypertext Preprocessor". Anyway, I was referring to the statement you made about DRY, and why it's, "Don't", and not, "do not". Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808750 Share on other sites More sharing options...
nrg_alpha Posted April 13, 2009 Share Posted April 13, 2009 Sometimes acronyms don't follow the rule, like PHP for example, "Hypertext Preprocessor". From my understanding, PHP = (Personal home page Hypertext Preprocessor) As initially PHP stood for Personal Home Page, but then that all got bunched into the first P. At least, that's what i read. Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808753 Share on other sites More sharing options...
Daniel0 Posted April 13, 2009 Share Posted April 13, 2009 Actually, PHP is a recursive acronym for "PHP: Hypertext Preprocessor" as of PHP 3. Like GNU (GNU's not Unix), Wine (Wine Is Not an Emulator), YAML (YAML Ain't Markup Language), etc. Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808772 Share on other sites More sharing options...
Mchl Posted April 13, 2009 Share Posted April 13, 2009 Anyway, I was referring to the statement you made about DRY, and why it's, "Don't", and not, "do not". Yeah i got that... I'm just having fun with acronyms now. I especially love those self-referencing ones.. like PHP - PHP Hypertext Preprocessor Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808778 Share on other sites More sharing options...
nrg_alpha Posted April 13, 2009 Share Posted April 13, 2009 Actually, PHP is a recursive acronym for "PHP: Hypertext Preprocessor" as of PHP 3. Like GNU (GNU's not Unix), Wine (Wine Is Not an Emulator), YAML (YAML Ain't Markup Language), etc. But doesn't that initial PHP represent the original 'Personal Home Page' (thus PHP for short)? Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808779 Share on other sites More sharing options...
Daniel0 Posted April 13, 2009 Share Posted April 13, 2009 Used to, but not anymore. That's why you call it a recursive acronym. The acronym is part of its definition, so if you spell out the acronym you get the acronym again, and you can then spell out that one, and the next one, etc. Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808781 Share on other sites More sharing options...
nrg_alpha Posted April 13, 2009 Share Posted April 13, 2009 Used to, but not anymore. That's why you call it a recursive acronym. The acronym is part of its definition, so if you spell out the acronym you get the acronym again, and you can then spell out that one, and the next one, etc. Ah yes, now I see that definition in Wikipedia. Certainly doesn't match the definition I once read, but it makes sense (due to the recursive bit). Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808788 Share on other sites More sharing options...
tibberous Posted April 13, 2009 Author Share Posted April 13, 2009 premiso, your way also works, I just think the other way is cleaner. Contexts could be used for other stuff too. Like $dollar could be referred to in a 'currency' context, that would automatically return '$'.number_format($x, 2); or maybe a string could be output in an html context, where html_entities are already applied. Its really just a short version of a function call. Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808954 Share on other sites More sharing options...
premiso Posted April 13, 2009 Share Posted April 13, 2009 I think you are doing this in vain. Really it is not much work to just do $string = dollar('50.34'); Vs $string:50.02; Granted, I still do not grasp the context syntax/terminology. But yea, I fail to see how it is "easier" than just using a function. Not to mention more confusing, at least with the function people know what to look for/refer to. I do not see how it would be easier/more efficient. Saving what, maybe 10 more characters. an extra 1 or 2 seconds of typing to make it less readable. Not worth it, in my opinion. Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-808960 Share on other sites More sharing options...
tibberous Posted April 14, 2009 Author Share Posted April 14, 2009 Contexts could be used in strings though. So, instead of saying: $message = "You have ".dollar($amount); You could say: $message = "You have $:amount"; A contexts name would always be one character long, and a symbol. It would always be defined as a function that takes one value and returns one value. Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-810087 Share on other sites More sharing options...
xylex Posted April 14, 2009 Share Posted April 14, 2009 Doing something like this to "lower the bar" is almost always a bad idea. Which is why I have no idea why GOTO was added in 5.3. If $: is defined by the specific script as a call to one specific function, where is the portability/reusability of the code? If you try to standardize it that $: is a synonym for mysql_real_escape_string, then what about using the code with other databases? If you use a library that overrides what the $: does, then what if that stops your data from being escaped? There are just way to many bad things that I could see happening to justify taking the risk to save an extra 10 keystrokes each time. Quote Link to comment https://forums.phpfreaks.com/topic/153855-i-have-the-best-idea-ever-variable-contexts/#findComment-810103 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.