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

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


当前回答

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

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

(仅PHP7.2)

其他回答

PHP代码有几个“编译器”。它们中的大多数不支持所有PHP特性,因为这些特性必须在运行时进行解释。

我们正在使用Phalanger - http://www.php-compiler.net/ -它甚至支持那些肮脏的PHP动态特性,并且仍然能够将它们编译为。net程序集,可以作为一个独立的DLL分发。

自从这个问题第一次被问到,答案已经从直截了当的“不”变成了“有点”

http://github.com/facebook/hiphop-php/wiki

Hip Hop for PHP是一个编译器,它将PHP代码转换为高度优化的c++ 显然,某些函数不受支持(例如' explosion ')

我在寻找更多关于如何实现HipHop的信息时发现了这个问题,我想我应该说出来:)

然而,自2013年以来,Facebook不再使用它,它已经停止使用,转而使用HHVM,这不是一个编译器:https://en.wikipedia.org/wiki/HipHop_for_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引擎上,因此您有“双重解释”。

也有

PHP的(实验性的)编译器扩展,

它的目标是

在私有PHP应用程序中编码整个脚本 在私有PHP应用程序中编码一些类和/或函数 使php-gtk应用程序能够在客户端桌面上使用,而不需要php.exe。 做一个PHP到C转换器的可行性研究

扩展可从PECL。