Take the 2-minute tour ×

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!

share|improve this question

2 Answers 2

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.

share|improve this answer
Exactly. A variable is just that, a variable. It changes with time. The compiler has no way of telling what it will be tomorrow so it doesn't try and mess up. –  frostymarvelous Jun 29 '11 at 18:50
Not quite. A good compiler should have some idea what the value of $foo is. And if it can figure that out, you don't need the switch statement; the compiler can fake it. If the compiler has NO clue what the value $foo is, then this is the problem as stated. One thing worth asking: if the programmer can code such a construct, how can he possibly know what the program does? So somewhere, there's a constraint on what $foo can be. Whether it is determinable by the compiler is another matter. One could simply have the compiler complain when it doesn't get enough information. –  Ira Baxter Jun 29 '11 at 19:06
@IRA. I think you are a bit off. This is a problem stated by the writer of phc as relating to hiphop. Yes, of course the compiler can figure out the variable contents, but it needs to compile a while script including all the used libraries. How does it do that when these libraries can be changed arbitrarily. Compile all possible runtime versions? –  frostymarvelous Jun 29 '11 at 19:28
How does it figure out the version being used at the time the script is called? Using a switch, it can include all libraries and compile. So whatever goes on, all the needed files are there. Please remember this is a HPHP specific question. –  frostymarvelous Jun 29 '11 at 19:31
Appreciate the responses everyone! I am a lot clearer now on why dynamic constructs are an issue. However, as @Magicianeer stated in the other answer, compiled languages do have the ability to use dynamic constructs? I posed the question to @Magicianeer in his response as well: can dynamic linkers and foreign function interfaces theoretically be built into php as well? What are the implications and reasons why it is not already built in to php? –  blacktie24 Jun 29 '11 at 19:41

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


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.

share|improve this answer
thx for the thoughtful reply! Based on the last sentence of your reply, is there anything stopping someone (with the knowhow) from writing these dynamic linkers and foreign function interfaces into the compiler? If not, can a php extension be built to allow this? –  blacktie24 Jun 29 '11 at 19:37
PHP has the 'dl()' function which allowed loading an arbitrary PHP extension. This has been disabled in 5.3 for security. It is effectively a dynamic linker that will load any library conforming to PHP extension interface, making its functions available to a PHP script. Hackers had too much fun :( An extension that can load a completely arbitrary .so, and call functions in it is possible... but very difficult to do usefully because you must manually identify argument data types and structures and convert PHP data to them. Its been done for Scheme and C++ (and maybe Perl). –  Magicianeer Jul 4 '11 at 4:17

Your Answer


