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

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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!

How to&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.

Answer:

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.