variable variable names

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

jshpro2 wrote:If you really needed your 'config' vars to be editable at runtime, it's a more solid solution to use a master array for configs, as opposed to variable variables (which can lead to issues)
Now we run directly into the tree called "Reality" in our idealic car.

The game uses *hundreds* of config variables across 30,000 lines of code. It is beyond unrealistic to change them all (and only those variables, not the other similarly named, similarly placed, and similarly scoped variables around them) to be $configure['something'].

While it is somewhat possible that the config variables don't get changed during the course of many files (favoring your define idea), it is not at all possible to change them all to a new prefixing scheme.

Idealism, meet the cold harsh reality of far too much legacy code.

Further, if I'm going that far down the road of idyllic lane, I'm going to a registry class, ya know? :)

update:
jshpro2 wrote:as opposed to variable variables (which can lead to issues)
Abstract fud must end. Cite an example. My code uses variable variables, without issue, and both of the alternatives have stated, agreed upon "issues". Please, be specific, or don't scare off people of a useful tool. :)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Hmm, I was just opening up the door for discussion not trying to get you to convert your code :wink:

By "it can lead to issues" I mean it can, using variables alone can lead to issues. The question here to ask is, what can the variable variables offer me that using a variable as an array key cannot

$array[$value] VS $$value

Using a variable for an array key can/will lead to just as many issues as the variable variables as I see it, but I cannot cite a specific example of confusion it has led to as I have never implemented it in a "real" application. To the extend of all research on the subject I see no advantage to it rather then using the variable array key method. What is the advantage of the array key method? maintainability. The next guy that comes along to tinker with my code might not have even heard of variable variables (I dont blame him either)

Provide me one scenario where the variable variables outweigh array key method (non aesthetic benefits), and you'll sell me.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

jshpro2 wrote:By "it can lead to issues" I mean it can, using variables alone can lead to issues.
Thats a very different statement. Yes, variables are variable.

But variable variables are no more prone to issues than a variable. (Say it five times fast!).
jshpro2 wrote:The question here to ask is, what can the variable variables offer me that using a variable as an array key cannot
In the case of one real-world legacy codebase used by thousands of people a day, it offers you a savings of thousands of hours of recoding and testing to ensure you recoded correctly. I'd call that both a real-world example, and a concrete advantage.
jshpro2 wrote:Using a variable for an array key can/will lead to just as many issues as the variable variables as I see it, but I cannot cite a specific example of confusion it has led to as I have never implemented it in a "real" application.
Aha. So its confusion you are concerned about when you say "issues". That much, we can completely agree with. Variable variables are definitely confusing.
jshpro2 wrote:To the extend of all research on the subject I see no advantage to it rather then using the variable array key method. What is the advantage of the array key method? maintainability. The next guy that comes along to tinker with my code might not have even heard of variable variables (I dont blame him either)
In the case of legacy code, its the polar opposite. Switching would take an unrealistic impact, and thus be unmaintainable.
jshpro2 wrote:Provide me one scenario where the variable variables outweigh array key method (non aesthetic benefits), and you'll sell me.
Done.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

I agree with everything you just said, but I am debating the advantages of using one over the other. Not the advantages of upgrading legacy code.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

jshpro2 wrote:I agree with everything you just said, but I am debating the advantages of using one over the other. Not the advantages of upgrading legacy code.
Well, since the conversation started, I've become much more interested in the define idea.. I may just give it a try later.
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post by php3ch0 »

Thanks people

My problem is with a quiz that I am developing. All of the quizzes and questions can be added in an admin area. There can be any number of options for each answer and some questions are multiple choice or have one answer. This means that when the question is displayed it will show radio buttons or check boxes repectively.

To send the arguments to another page (check_question.php) it uses $_POST.

each radio button or check box will have a name from the database primary key. e.g

Code: Select all

<?php if ($row_question['multi_options'] == 'N') { ?>
                   <input name="radiobutton" type="radio" class="h2blue" value="option<?php echo $row_options['id']; ?>"><?php } else { ?>
                          <input name="option<?php echo $row_options['id']; ?>" type="checkbox" class="h2blue" id="option<?php echo $row_options['id']; ?>" value="Y"><?php } ?>
When it passes this information to check_question.php I need to know the names of the $_POST variables.
I plan to do this using the vairable variable names.

I read you stuff about arrays would it be better to do this? if so how?
Post Reply