Well, you can do
$moo = "hello";
$zork = "moo";
echo $$zork; // hello
It doesn't get much more dynamic than that. $moo = "hello";
$zork = "moo";
echo $$zork; // hello
It doesn't get much more dynamic than that.Although PHP does support the above syntax, it's actually pretty rare in production code. PHP can be optimized like JavaScript is: perform direct variable/member access but provide a slow path for these kinds of dynamic lookups.
<?php
eval('class Hello {public static function you(){echo "Yes!";}}');
Hello::you();
or this: <?php
file_put_contents('c.php', '<?php class Hello {public static function you(){echo "Yes!";}}');
require('c.php');
Hello::you();
Thus generating a class at runtime-ish.With a proper intermediate representation you'd connect that $$ variable lookup directly to the original variable's edge.
If not please correct me.
echo $FOO[$BAR];
echo $$BAZ;
>>>
push FOO.ptr
push BAR.ptr
call hash_get
push ax
call print
push local_table // static per compiled scope
push BAZ.ptr
call hash_get
push [ax+24] // entry->value
call print
You'd probably want to add error handling (to both cases) and accessing a global variable that didn't exist when the code was compiled will likely be slow, but if becomes a preformance bottleneck you can always rewrite it to something easier to compile.