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

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


当前回答

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

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

其他回答

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

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

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

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

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的当前实现是一种解释性语言。您可以从理论上论证任何语言在技术上都可以被解释或编译的事实,但就目前的情况而言,当前的实现是PHP代码需要一个解释器才能运行,解释器管理执行环境。

要回答关于上传预编译的PHP字节码的问题,这可能是可行的,但必须实现一种方法,让PHP解释器读取这样的文件并使用它。由于已有的操作码缓存已经存在,这似乎不是一个会获得太多回报的任务。

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.