Page 3 of 3

Re: Variable variables

Posted: Mon Oct 04, 2010 12:26 pm
by JoeCommodore
John Cartwright wrote:
JoeCommodore wrote:Here is my example:

This is the type of stuff I use it for the most - reading in db fields into variables:

Code: Select all

    $things = mysql_fetch_assoc($result); 
    foreach($things as $key => $value) {
        $$key = $value;
    }
Same goes for initializing and reading in filtered POST data into variables (from a an array of POST variable names I specify)
Reminds me of register globals. Why do we hate arrays so much!?
With register globals you don't have any control over what you port in, that why register globals a bad thing. With this type of method you can set conditions and determine what happens, in the case above moving a database record to variables (add extra filtering as needed) saves a ton of code and to me looks easy to understand.

Arrays are OK, but in my mind using it for everything adds a layer to working with the data, and could affect overall performance. I do love arrays, and use them a lot, as arrays.

Re: Variable variables

Posted: Mon Oct 04, 2010 12:35 pm
by John Cartwright
JoeCommodore wrote:With register globals you don't have any control over what you port in, that why register globals a bad thing. With this type of method you can set conditions and determine what happens, in the case above moving a database record to variables (add extra filtering as needed) saves a ton of code and to me looks easy to understand.

Arrays are OK, but in my mind using it for everything adds a layer to working with the data, and could affect overall performance. I do love arrays, and use them a lot, as arrays.
Just so were clear, for the sake of this argument, we are talking about user input.

So what you are saying is, taking an array, specifically filtering only certain inputs, then converting it to standalone variables is adding a layer to work with (and affects performance -- although performance is highly irrelevant for such a small operation)

.. versus..

Just using the array PHP generates for you.

Your argument seems a little backwards.

Re: Variable variables

Posted: Mon Oct 04, 2010 12:48 pm
by alex.barylski

Code: Select all

$things = mysql_fetch_assoc($result); 
foreach($things as $key => $value) {
  $$key = $value;
}
What is this code doing? Simply bringing the array elements into scalars? In that case, why not simply pass the aswsociative array to extract:

http://ca2.php.net/manual/en/function.extract.php

Cheers,
Alex

Re: Variable variables

Posted: Mon Oct 04, 2010 4:15 pm
by JoeCommodore
John Cartwright wrote:
JoeCommodore wrote:With register globals you don't have any control over what you port in, that why register globals a bad thing. With this type of method you can set conditions and determine what happens, in the case above moving a database record to variables (add extra filtering as needed) saves a ton of code and to me looks easy to understand.

Arrays are OK, but in my mind using it for everything adds a layer to working with the data, and could affect overall performance. I do love arrays, and use them a lot, as arrays.
Just so were clear, for the sake of this argument, we are talking about user input.

So what you are saying is, taking an array, specifically filtering only certain inputs, then converting it to standalone variables is adding a layer to work with (and affects performance -- although performance is highly irrelevant for such a small operation)

.. versus..

Just using the array PHP generates for you.

Your argument seems a little backwards.
Not to me, I consider anything in POST to be unfiltered, anything I choose to pull out of POST must be processed and then I know it is good. So either way I move stuff out of POST before dealing with it, array or otherwise.

I understand your perspective on using arrays, I just don't work that way presently.

Re: Variable variables

Posted: Mon Oct 04, 2010 8:25 pm
by John Cartwright
JoeCommodore wrote:Not to me, I consider anything in POST to be unfiltered, anything I choose to pull out of POST must be processed and then I know it is good. So either way I move stuff out of POST before dealing with it, array or otherwise.

I understand your perspective on using arrays, I just don't work that way presently.
Dealing with user input and filtering is beyond the scope of this discussion, and is certainly a valid requirement. My problem with your approach is that I prefer not to liter the global (in it's scope) namespace with variables from user input.

My point is, you are opening yourself up to name collissions. You have to remember, when you are dealing with software, even if you are a sole programmer on a project, there is always the potential of coming back to the project weeks, months, or even years later to make alterations. Now lets say another programmer comes along and adds new input fields, but does not realize you are using that variable for something else, and bang, a bug.

By filtering which variables you introduce to the global namespace, you are only lessing the impact of a feature which is widely adopted as bad practice. I just makes more sense to me to avoid any possible bugs.

Re: Variable variables

Posted: Mon Oct 04, 2010 9:44 pm
by JoeCommodore
John Cartwright wrote:
JoeCommodore wrote:Not to me, I consider anything in POST to be unfiltered, anything I choose to pull out of POST must be processed and then I know it is good. So either way I move stuff out of POST before dealing with it, array or otherwise.

I understand your perspective on using arrays, I just don't work that way presently.
Dealing with user input and filtering is beyond the scope of this discussion, and is certainly a valid requirement. My problem with your approach is that I prefer not to liter the global (in it's scope) namespace with variables from user input.

My point is, you are opening yourself up to name collissions. You have to remember, when you are dealing with software, even if you are a sole programmer on a project, there is always the potential of coming back to the project weeks, months, or even years later to make alterations. Now lets say another programmer comes along and adds new input fields, but does not realize you are using that variable for something else, and bang, a bug.

By filtering which variables you introduce to the global namespace, you are only lessing the impact of a feature which is widely adopted as bad practice. I just makes more sense to me to avoid any possible bugs.
I admit you do make a compelling argument, and I will keep it in mind in future development, thanks... hmm... :)

Re: Variable variables

Posted: Mon Oct 04, 2010 11:13 pm
by Benjamin
There's also the issue of debugging/readability. If you needed to get a list of what data is available you could simply print_r or var_dump the array. If all the variables were separate you would need to read the code and find each one or do some further reverse engineering to see what is all getting set. Also, when calling other functions/classes every single variable would need to be passed to it instead of just one array. It really wouldn't even be practical to write methods that can receive an unknown number of arbitrarily named variables either.

Really it's a messy way to do things and it's certainly not the easy way. I can't see any reason to do this.

That said however, I'm not writing off variable variables. There's a time and a place for everything. PHP is a versatile language, just knowing when to use what is the hard/fun part.

Re: Variable variables

Posted: Tue Oct 05, 2010 9:34 am
by alex.barylski
+1 for everything said by jcart