> making a dynamic language perform well
Perhaps the advantage here is that PHP really all that dynamic. PHP is much closer in design to taking Java and compiling it on every request than it is like Python or Ruby.
Perhaps the advantage here is that PHP really all that dynamic. PHP is much closer in design to taking Java and compiling it on every request than it is like Python or Ruby.
$moo = "hello";
$zork = "moo";
echo $$zork; // hello
It doesn't get much more dynamic than that.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.With a proper intermediate representation you'd connect that $$ variable lookup directly to the original variable's edge.
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.The _get and _set magic methods (called when getting/setting missing properties) are quite common especially for frameworks.