variable-variables

Variable variables: when useful? [duplicate]

旧街凉风 提交于 2019-11-28 01:41:10
问题 Possible Duplicate: What's an actual use of variable variables? OK, this question may look a little bit point-whoreish, but I'd really like to know: when are variable variables useful? I haved programmed in PHP for several years, but I've never used them. For me it looks rather funny than useful. What are some real life examples of variable variables? Update: I'm really sorry about your votes. The linked 'duplicate' may be really a dupe except one thing: the examples listed there show me why

java String to class

。_饼干妹妹 提交于 2019-11-27 18:07:10
问题 I have got a bean class named Bean1 . In my main method I have got a string containing the name of the variable: String str= "Bean1"; Now how can I use the String variable to get the class and access the Bean properties? 回答1: Step by step: //1. As Kel has told you (+1), you need to use //Java reflection to get the Class Object. Class c = Class.forName("package.name.Bean1"); //2. Then, you can create a new instance of the bean. //Assuming your Bean1 class has an empty public constructor:

Variable Variables Pointing to Arrays or Nested Objects

时光总嘲笑我的痴心妄想 提交于 2019-11-27 16:16:31
Is it possible to create a variable variable pointing to an array or to nested objects? The php docs specifically say you cannot point to SuperGlobals but its unclear (to me at least) if this applies to arrays in general. Here is my try at the array var var. // Array Example $arrayTest = array('value0', 'value1'); ${arrayVarTest} = 'arrayTest[1]'; // This returns the correct 'value1' echo $arrayTest[1]; // This returns null echo ${$arrayVarTest}; Here is some simple code to show what I mean by object var var. ${OBJVarVar} = 'classObj->obj'; // This should return the values of $classObj->obj

What's an actual use of variable variables?

依然范特西╮ 提交于 2019-11-27 09:06:39
Variable variables seem pretty cool, but I can't think of a scenario where one would actually use them in a production environment. What would such a scenario be? How were they used? Its purpose, I guess, is to allow novice programmers to dynamically change data without using "complicated stuff" like composite types (arrays and objects). I never use them. A variable variable is essentially an array (map/dictionary). The following are equivalent ideas: <?php $foo = array('a' => 1); $bar = 'a'; echo $foo[$bar]."\n"; $foo_a = 1; $bar = 'a'; $vv = "foo_$bar"; echo $$vv."\n"; ?> Thus if you wrap

How do I import variable packages in Python like using variable variables ($$) in PHP?

孤者浪人 提交于 2019-11-27 06:40:54
I want to import some package depending on which value the user chooses. The default is file1.py : from files import file1 If user chooses file2 , it should be : from files import file2 In PHP, I can do this using variable variables : $file_name = 'file1'; include($$file_name); $file_name = 'file2'; include($$file_name); How can I do this in Python? Python doesn't have a feature that's directly equivalent to PHP's "variable variables". To get a "variable variable"'s value (or the value of any other expression) you can use the eval function. foo = "Hello World" print eval("foo") However, this

Variable variable class extensions in PHP--is it possible?

那年仲夏 提交于 2019-11-27 04:43:11
问题 Is something like the following possible in PHP? $blah = 'foo1'; class foo2 extends $blah { //... } class foo1 { //... } This gives an error. I want to dynamically set $blah so I can extend whatever class I want. Edit: The reason for wanting to do this because I wanted to use a function out of another class in a related class. In the end it would have been something like: Final extends foo1 extends foo2 extends foo3 extends foo4 extends parent { ... } In the end I decided to instantiate the

How do I dynamically create the variable name in a PHP loop?

耗尽温柔 提交于 2019-11-27 02:48:51
问题 Ok so i have this php foreach loop <?php foreach ($step_count->rows as $step) { ?> and $step will be the step numbers 1, 2, 3, 4, 5 up to the total steps within the loop i a need to set the value of the images within the loop to standard_image_1 or whatever step there is...so for example <input value="<?php echo {$standard_image_"$step['number']"}; ?>" /> so basically i need the variable $standard_image_1 and so on depending on the steps but i dont know the correct syntax to do this 回答1: Look

Variable Variables Pointing to Arrays or Nested Objects

无人久伴 提交于 2019-11-26 22:26:35
问题 Is it possible to create a variable variable pointing to an array or to nested objects? The php docs specifically say you cannot point to SuperGlobals but its unclear (to me at least) if this applies to arrays in general. Here is my try at the array var var. // Array Example $arrayTest = array('value0', 'value1'); ${arrayVarTest} = 'arrayTest[1]'; // This returns the correct 'value1' echo $arrayTest[1]; // This returns null echo ${$arrayVarTest}; Here is some simple code to show what I mean

what is “$$” in PHP

回眸只為那壹抹淺笑 提交于 2019-11-26 18:22:22
问题 I saw this code if (is_null($$textVarName)) { $$textVarName = $_defaultTexts[$type]; } what is code "$$" ? 回答1: It's evil is what it is. That will take the value that's in $textVarName and use that as a variable name. For example: $foo = 'hello'; $hello = 'The Output'; echo $$foo; // displays "The Output" 回答2: foreach($_POST as $key=>$value)$$key=$value; now, automagically, if the previous form had a field named 'username' you now have a variable called $username that holds the value

What&#39;s an actual use of variable variables?

↘锁芯ラ 提交于 2019-11-26 14:29:57
问题 Variable variables seem pretty cool, but I can't think of a scenario where one would actually use them in a production environment. What would such a scenario be? How were they used? 回答1: Its purpose, I guess, is to allow novice programmers to dynamically change data without using "complicated stuff" like composite types (arrays and objects). I never use them. 回答2: A variable variable is essentially an array (map/dictionary). The following are equivalent ideas: <?php $foo = array('a' => 1);