variable-variables

variable-variables in PHP

ε祈祈猫儿з 提交于 2019-12-18 08:30:12
问题 I know you can do: $hash('foo') and $$foo and also $bar[$foo] , what are each of these things called? 回答1: $hash('foo') is a variable function. $hash may contain a string with the function name, or an anonymous function. $hash = 'md5'; // This means echo md5('foo'); // Output: acbd18db4cc2f85cedef654fccc4a4d8 echo $hash('foo'); $$foo is a variable variable. $foo may contain a string with the variable name. $foo = 'bar'; $bar = 'baz'; // This means echo $bar; // Output: baz echo $$foo; $bar[

How would I call a method from a class with a variable?

故事扮演 提交于 2019-12-13 15:25:57
问题 Given this class: class Tacobell{ public function order_taco(){ echo "3 Tacos, thank you."; } public function order_burrito(){ echo "Cheesy bean and rice, please"; } } $lunch = new Tacobell; $lunch->order_burrito(); $lunch->order_taco(); How would I do something like this? $myOrder = 'burrito'; $lunch->order_.$myOrder; Obviously that code is bunk--but shows what I'm attempting to do better than trying to explain it away. And maybe I'm going about this all wrong. I thought about a method with

PHP: Class property chaining in variable variables

北战南征 提交于 2019-12-12 09:56:31
问题 So, I have a object with structure similar to below, all of which are returned to me as stdClass objects $person->contact->phone; $person->contact->email; $person->contact->address->line_1; $person->contact->address->line_2; $person->dob->day; $person->dob->month; $person->dob->year; $album->name; $album->image->height; $album->image->width; $album->artist->name; $album->artist->id; etc... (note these examples are not linked together). Is it possible to use variable variables to call contact-

How to check if a string can be used as a variable name in PHP?

≯℡__Kan透↙ 提交于 2019-12-12 08:01:40
问题 In PHP one can use variable variables... For example... class obj { } $fieldName = "Surname"; $object = new obj(); $object->Name = "John"; $object->$fieldName = "Doe"; echo "{$object->Name} {$object->Surname}"; // This echoes "John Doe". However, $fieldName string may contain some characters not allowed in variable names. PHP will still create the field with that name (much like the associative array), but I will not be able to access it with $object->...... because it would not parse

Is Variable Variable Possible?

痞子三分冷 提交于 2019-12-11 05:12:41
问题 My googlefu has failed me and I come to you for help: Is VBA capable of having Variable Variables like PHP? I know that in PHP you can wait to declare a variable by using $$var . Is it possible to do it in VBA? for example, is there a way that lets say: I read an entire array of 1000 strings and each string that I get can declare a variable with that string, e.g if the 80th element of an array is named STO how can I tell VBA to create a variable with the name sto? 回答1: It's not possible. But

is it bad practice to use variable variables in php in the following fashion?

荒凉一梦 提交于 2019-12-05 16:25:06
For example, a simple mvc type system: /api/class/method rewritten into php variables using .htaccess/nginx.conf then doing something like: <?php // Set up class + method variables $className = some_class_filter($_GET['class']); $method = some_method_filter($_GET['method']); // Check if class exists and execute if(file_exists(BASE . "/controllers/" . $className . ".class.php")) { require BASE . "/controllers/" . $className . ".class.php"; $$className = new $className(); // Execute the method $$className->$method(); } else { // Spit out some error based on the problem } ?> Is this horribly bad

How to check if a string can be used as a variable name in PHP?

会有一股神秘感。 提交于 2019-12-03 15:09:54
In PHP one can use variable variables... For example... class obj { } $fieldName = "Surname"; $object = new obj(); $object->Name = "John"; $object->$fieldName = "Doe"; echo "{$object->Name} {$object->Surname}"; // This echoes "John Doe". However, $fieldName string may contain some characters not allowed in variable names. PHP will still create the field with that name (much like the associative array), but I will not be able to access it with $object->...... because it would not parse correctly. Now, is there any function that can check if the string can be used as a valid PHP variable name.

can array values be accessed by variable variables?

纵然是瞬间 提交于 2019-12-02 17:12:47
问题 I have an array which I can only access correctly via variable variables, like so: $foo['bar'] = "pie"; $fixed_name_variable = "foo['bar']"; echo $$fixed_name_variable; Which in theroy echo's pie . Except it's just not returning anything. So I need to know if this approach is actually workable or if I need a rethink on it. Just noticed. On the second line, should the bar be in quotes? 回答1: Although I hate to encourage this behaviour, you can use eval to achieve what you to a limited extent.

can array values be accessed by variable variables?

不问归期 提交于 2019-12-02 09:36:41
I have an array which I can only access correctly via variable variables, like so: $foo['bar'] = "pie"; $fixed_name_variable = "foo['bar']"; echo $$fixed_name_variable; Which in theroy echo's pie . Except it's just not returning anything. So I need to know if this approach is actually workable or if I need a rethink on it. Just noticed. On the second line, should the bar be in quotes? Although I hate to encourage this behaviour, you can use eval to achieve what you to a limited extent. $foo['bar'] = "pie"; $fixed_name_variable = "foo['bar']"; $a = eval("return $$fixed_name_variable;"); echo $a

Python variable variables without eval?

会有一股神秘感。 提交于 2019-12-02 02:46:59
问题 Is there is a way to access variables using a variable string in python? For instance, I would like a neater way than using eval for the following: def toggleListButtons (self): buttons = ["flip", "remove", "removeAll", "delete", "deleteAll", "loadDirectory"] for button in buttons: eval("self." + button + "Button.setEnabled(!self." + button + "Button.isEnabled())") 回答1: What you're looking for is the getattr() built-in function. There is also hasattr() and setattr(). button = getattr(self,