Overview
In PHP, you can just put an extra $
in front of a variable to make it a dynamic variable :
Nội dung chính
- Differences between PHP5 and PHP7
- Case 1 : $$foo['bar']['baz']
- Case 2 : $foo->$bar['baz']
- Case 3 : $foo->$bar['baz'][]
- Case 4 : Foo::$bar['baz'][]
- 5.5.3. Discussion
$$variableName = $value;
While I wouldn't recommend it, you could even chain this behavior :
$$$$$$$$DoNotTryThisAtHomeKids = $value;
You can but are not forced to put $variableName
between {}
:
${$variableName} = $value;
Using {}
is only mandatory when the name of your variable is itself a composition of multiple values, like this :
${$variableNamePart1 . $variableNamePart2} = $value;
It is nevertheless recommended to always use {}
, because
it's more readable.
Differences between PHP5 and PHP7
Another reason to always use {}
, is that PHP5 and PHP7 have a slightly different way of dealing with dynamic variables, which results in a different outcome in some cases.
In PHP7, dynamic variables, properties, and methods will now be evaluated strictly in left-to-right order, as opposed to the mix of special cases in PHP5. The examples below show how the order of evaluation has changed.
Case 1 : $$foo['bar']['baz']
- PHP5 interpetation :
${$foo['bar']['baz']}
- PHP7 interpetation :
${$foo}['bar']['baz']
Case 2 : $foo->$bar['baz']
- PHP5 interpetation :
$foo->{$bar['baz']}
- PHP7 interpetation :
$foo->{$bar}['baz']
Case 3 : $foo->$bar['baz'][]
- PHP5 interpetation :
$foo->{$bar['baz']}[]
- PHP7 interpetation :
$foo->{$bar}['baz'][]
Case 4 : Foo::$bar['baz'][]
- PHP5 interpetation :
Foo::{$bar['baz']}[]
- PHP7 interpetation :
Foo::{$bar}['baz'][]
5.5.3. Discussion
The previous example prints 103. Because $animal = 'turtles', $$animal is $turtles, which equals 103.
Using curly braces, you can construct more complicated expressions that indicate variable names:
$stooges = array['Moe','Larry','Curly']; $stooge_moe = 'Moses Horwitz'; $stooge_larry = 'Louis Feinberg'; $stooge_curly = 'Jerome Horwitz'; foreach [$stooges as $s] { print "$s's real name was ${'stooge_'.strtolower[$s]}.\n"; } Moe's real name was Moses Horwitz. Larry's real name was Louis Feinberg. Curly's real name was Jerome Horwitz.
PHP evaluates the expression between the curly braces and uses it as a variable name. That expression can even have function calls in it, such as strtolower[ ].
Variable variables are also useful when iterating through similarly named variables. Say you are querying a database table that has fields named title_1, title_2, etc. If you want to check if a title matches any of those values, the easiest way is to loop through them like this:
for [$i = 1; $iObject methods can also be called with the variable functions syntax.
Example #2 Variable method example
When calling static methods, the function call is stronger than the static property operator:
Example #3 Variable method example with static properties
Example #4 Complex callables
niemans at pbsolo dot nl ¶
3 years ago
While the documentation suggests that the use of a constant is similar to the use of a variable, there is an exception regarding variable functions. You cannot use a constant as the function name to call a variable function.
const DEBUGME ='func';
function func[$s] { echo $s. "\n"; }
DEBUGME['abc']; // results in a syntax error
$call = DEBUGME;
$call['abc']; // does the job
But you can use a constant as an argument to a function. Here's a simple workaround when you need to call a variable constant function:
function dynamic[$what, $with]
{
$what[$with];
}
dynamic[DEBUGME, 'abc'];
This makes sense to me to hide API's and/or long [complicated] static calls.
Enjoy!
Anonymous ¶
11 years ago
$ wget //www.php.net/get/php_manual_en.tar.gz/from/a/mirror
$ grep -l "\$\.\.\." php-chunked-xhtml/function.*.html
List of functions that accept variable arguments.
rnealxp at yahoo dot com ¶
2 years ago
anisgazig at gmail dot com ¶
6 months ago
It works, but $choice is not what you might think, a reference to a function. It is simply the name of the function as a string, written without [!] quotes.
It's the same as
You can do echo gettype[$choice] to confirm.
So calling
is a variable-function for both cases, calling it by its name, not by reference.
Go via an assigned anonymous function to get a reference to the function:
Now you can pass around the function like a first class object
or
and call it
If you want to pass around a class method, use the "Complex callables" from the manual, above. It's a call by name [not a reference], but since you can include the object you can still get the flexibility you want:
You can use $this as the object in the first element of the array.
And now, le moment supreme:
josh at joshstroup dot xyz ¶
6 years ago
A small, but helpful note. If you are trying to call a static function from a different namespace, you must use the fully qualified namespace, even if they have the same top level namespace[s]. For example if you have the following class to call:
You must call it as:
and not: