简体   繁体   English

为什么 php 编译器 (HPHP) 难以动态构造?

[英]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.我正在阅读 Paul Bigger 的http://blog.paulbiggar.com/archive/a-rant-about-php-compilers-in-general-and-hiphop-in-particular/他提到 HPHP 并不完全支持动态构造。 He then states, "Still, a naive approach is to just stick a switch statement in, and compile everything that makes sense."然后他说:“不过,一种天真的方法就是插入一个 switch 语句,然后编译所有有意义的东西。” Is he saying that instead of a dynamic include, you could use switch statements to include the proper file?他是说您可以使用 switch 语句来包含正确的文件,而不是动态包含吗? If so, why would this work and why is it "easier" for a compiler to compile?如果是这样,为什么这会起作用,为什么编译器“更容易”编译? As always, thx for your time!一如既往,感谢您的时间!

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.他们可以简单地编译“bar”和“quux”并将它们包装在一个 if 语句中,该语句检查$foo并执行任何适当的操作。

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.如果 $random_file 中命名的文件声明了常量、类、变量,编译器将无法知道,因为 $random_file 的值在编译时是未知的。 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. switch 语句将知道可能的文件列表,以便编译器可以发现任何相关的声明。

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.设计用于编译的语言具有动态链接器和外部 function 接口,它们结合起来提供类似的功能来 include($random_file) 而不需要显式开关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM