我知道PHP在服务器上运行之前被编译为字节代码,然后字节代码可以被缓存,这样整个脚本就不必在每次web访问时重新解释。

但是,您能否“编译”PHP代码并上传一个二进制文件,该文件将由字节码解释器运行?


当前回答

在php 7中有php ini选项opcache。File_cache,用于将字节码保存到指定文件夹中。In可能对In php cli脚本有用,这些脚本被“编译”并保存在特定的文件夹中,以便优化重用。

Opcache,它不是编译,而是类似的东西。

其他回答

还有bcgen (bcompiler的一个PHP7端口):

https://github.com/vjardin/bcgen/

(仅PHP7.2)

有人听说过Zend Guard吗,它的功能正是这个人想要的。它将PHP代码编码/混淆为“机器代码”。

PHP不像许多程序那样真正被编译。您可以使用Zend的编码器使其不可读。

如果你被允许运行真正的本地二进制文件,那么这是你的编译器:

https://github.com/ircmaxell/php-compiler

这是一个用PHP编写的PHP编译器!

It compiles PHP code to its own VM code. This VM code can then either be interpreted by its own interpreter (also written in PHP, isn't that crazy?) or it can be translated to Bitcode. And using the LLVM compiler framework (clang and co), this Bitcode can be compiled into a native binary for any platform that LLVM supports (pretty much any platform that matters today). You can choose to either do that statically or each time just before the code is executed (JIT style). So the only two requirements for this compiler to work on your system is an installed PHP interpreter and an installed clang compiler.

如果您不允许运行本机二进制文件,您可以使用上面的编译器作为解释器,让它解释自己的VM代码,但是这会很慢,因为您运行的PHP解释器本身运行在PHP引擎上,因此您有“双重解释”。

见5.5。使用集成的OPcache模块,在共享内存中的易失性,更好的性能和php的动态原则保持不变。

http://www.php.net/manual/en/opcache.installation.php