我知道PHP在服务器上运行之前被编译为字节代码,然后字节代码可以被缓存,这样整个脚本就不必在每次web访问时重新解释。
但是,您能否“编译”PHP代码并上传一个二进制文件,该文件将由字节码解释器运行?
我知道PHP在服务器上运行之前被编译为字节代码,然后字节代码可以被缓存,这样整个脚本就不必在每次web访问时重新解释。
但是,您能否“编译”PHP代码并上传一个二进制文件,该文件将由字节码解释器运行?
当前回答
在回答了这个问题之后,Facebook为PHP推出了HipHop,这可能是迄今为止测试最好的PHP编译器(因为它运行的是世界上最大的10个网站之一)。然而,Facebook为了支持HHVM而停止了它,HHVM是一个虚拟机,而不是编译器。
除此之外,谷歌PHP编译器会出现许多第三方解决方案。
PeachPie
PeachPie GitHub 编译PHP到。net和。net核心 可以编译成自包含二进制文件 运行在Mac, Linux, Windows, Windows核心,ARM,…
袋貂
GitHub(下载),Wikipedia 编译到。net (CIL)从2017年7月开始停产,似乎不支持PHP 7。
phc
编译为本地二进制文件 现在不是很活跃(2014年2月)-上一个版本在2011年,最后一次更改在2013年夏天
Roadsend PHP编译器
GitHub的一个重写 免费的,开源的PHP编译器实现 编译为本地二进制文件(Windows, Linux) 自2010年停用,直到贡献者发现-网站关闭,留在GitHub上,上一次更改是在2012年初
b编译器
PHP的PECL扩展 实验 编译为PHP字节码,但可以将其包装为加载PHP解释器的Windows二进制文件(参见bcompiler_write_exe_footer()手册) 现在已经停产了(2014年2月)——2011年最后一次更换
Project Zero
维基百科,IBM WebSphere sMash的变化孵化器 IBM支持 编译成Java字节码 现在看起来已经停止了(2014年2月)-网站关闭,看起来像2008年和2009年的大炒作
Bambalam
编译成独立的Windows二进制文件 二进制文件包含字节码和启动器 现在已经停产了(2014年2月)——2006年最后一次更换
二进制PHP
编译为c++ 现在看来已经停产了(2014年2月)——上一次更换是在2003年
其他回答
Actually, the Just-In-Time compiler introduced with PHP 8 does in fact compile PHP. Strangely enough, it doesn't really speed up CMS based websites (e.g. WordPress), however, it does open the doors for PHP to compete with the likes of C++. For more information, see the RFC behind the JIT implementation here: https://wiki.php.net/rfc/jit. Also, Matthew Weir O'Phinney has posted a number of insightful blogs that shed light on its capabilities. Start reading here: https://www.zend.com/blog/exploring-new-php-jit-compiler.
如果你被允许运行真正的本地二进制文件,那么这是你的编译器:
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。
PHP代码有几个“编译器”。它们中的大多数不支持所有PHP特性,因为这些特性必须在运行时进行解释。
我们正在使用Phalanger - http://www.php-compiler.net/ -它甚至支持那些肮脏的PHP动态特性,并且仍然能够将它们编译为。net程序集,可以作为一个独立的DLL分发。
如果您只是想从PHP脚本生成二进制可执行文件,那么请避免试图使您的问题非常精确,因为这会让人觉得您确实知道自己需要什么。此外,大多数PHP开发人员完全不知道字节码是什么。
话虽如此,答案是肯定的。我刚刚将一个PHP脚本编译成二进制文件。而不是任何二进制。我已经使用CDE应用程序(链接到Wayback Machine,原来的链接现在被破坏了)把它变成一个可移植的二进制文件,可以与所有依赖项一起分发,并且执行起来没有任何问题……而且它工作得很漂亮。
你所需要的就是使用phc。