Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are dynamic constructs difficult for php compilers (HPHP)?

I was reading up on Paul Bigger's http://blog.paulbiggar.com/archive/a-rant-about-php-compilers-in-general-and-hiphop-in-particular/ and he mentions that HPHP doesn't fully support dynamic constructs. He then states, "Still, a naive approach is to just stick a switch statement in, and compile everything that makes sense." Is he saying that instead of a dynamic include, you could use switch statements to include the proper file? If so, why would this work and why is it "easier" for a compiler to compile? As always, thx for your time!

like image 789
blacktie24 Avatar asked Jun 29 '11 18:06

blacktie24


2 Answers

from my understanding, if you've got this

 include "$foo.php";

the compiler would have no clue what you're going to include. On the other side, with this

  switch($foo) {
     case 'bar'  : include "bar.php";
     case 'quux' : include "quux.php";
  }

they can simply compile "bar" and "quux" and wrap them in an if statement which checks $foo and executes whatever is appropriate.

like image 57
user187291 Avatar answered Nov 14 '22 22:11

user187291


A compiler expects to be able to identify all of the source and binary files that might be used by the program being compiled.

include($random_file); 

If the file named in $random_file declares constants, classes, variables, the compiler will have no way knowing because the value of $random_file is not known at compile time. Your code using those constants, classes and variables will fail in difficult-to-debug ways. The switch statement would make known the list of possible files so the compiler can discover any relevant declarations.

Languages designed to be compiled have dynamic linkers and foreign function interfaces that combine to provide similar functionality to include($random_file) without needing the explicit switch.

like image 33
Magicianeer Avatar answered Nov 14 '22 23:11

Magicianeer