Jump to content

I have the best idea ever! Variable "contexts"!


tibberous

Recommended Posts

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

 

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

Do Repeat Yourself? Just guessing :D

 

(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".

Link to comment
Share on other sites

Ok... so DNR stands for 'Don't resuscitate' or for 'Don't not resuscitate' ? :P

 

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".

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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)?

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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. 

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.