您可以“编译”PHP 代码并上传一个二进制文件,该文件将仅由字节码解释器运行吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1408417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:26:56  来源:igfitidea点击:

Can you "compile" PHP code and upload a binary-ish file, which will just be run by the byte code interpreter?

phpbytecode

提问by Carson Myers

I know that PHP is compiled to byte code before it is run on the server, and then that byte code can be cached so that the whole script doesn't have to be re-interpreted with every web access.

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

But can you "compile" PHP code and upload a binary-ish file, which will just be run by the byte code interpreter?

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

回答by Frank Farmer

After this question was asked, Facebook launched HipHop for PHPwhich is probably the best-tested PHP compiler to date (seeing as it ran one of the world's 10 biggest websites). However, Facebook discontinued it in favour of HHVM, which is a virtual machine, not a compiler.

在提出这个问题后,Facebook 推出了HipHop for PHP,这可能是迄今为止经过最佳测试的 PHP 编译器(因为它运行着世界 10 大网站之一)。然而,Facebook 停止了它,取而代之的是 HHVM,它是一个虚拟机,而不是一个编译器。

Beyond that, googling PHP compilerturns up a number of 3rd party solutions.

除此之外,谷歌搜索PHP compiler发现了许多 3rd 方解决方案。

PeachPie

桃子派

  • PeachPie GitHub
  • compiles PHP to .NET and .NET Core
  • can be compiled into self-contained binary file
  • runs on Mac, Linux, Windows, Windows Core, ARM, ...
  • 桃派 GitHub
  • 将 PHP 编译为 .NET 和 .NET Core
  • 可以编译成自包含的二进制文件
  • 在 Mac、Linux、Windows、Windows Core、ARM、...

Phalanger

法郎格

phc

医生

  • compiles to native binaries
  • not very active now (February 2014) – last version in 2011, last change in summer 2013
  • 编译为本机二进制文件
  • 现在不是很活跃(2014 年 2 月)– 2011 年的最后一个版本,2013 年夏季的最后一次更改

Roadsend PHP Compiler

路信 PHP 编译器

bcompiler

编译器

  • PECL extension of PHP
  • experimental
  • compiles to PHP bytecode, but can wrap it in Windows binary that loads PHP interpreter (see bcompiler_write_exe_footer()manual)
  • looks discontinued now (February 2014) – last change in 2011
  • PHP 的 PECL 扩展
  • 实验性的
  • 编译为 PHP 字节码,但可以将其包装在加载 PHP 解释器的 Windows 二进制文件中(参见bcompiler_write_exe_footer()手册
  • 外观现已停产(2014 年 2 月)——2011 年的最后一次更改

Project Zero

零计划

  • Wikipedia, IBM
  • incubator of changes for WebSphere sMash
  • supported by IBM
  • compiles to Java bytecode
  • looks discontinued now (February 2014) – website down, looks like big hype in 2008 and 2009
  • 维基百科IBM
  • WebSphere sMash的变化孵化器
  • IBM 支持
  • 编译为 Java 字节码
  • 现在看起来已经停产(2014 年 2 月)——网站关闭,看起来像是 2008 年和 2009 年的大炒作

Bambalam

班巴拉姆

  • compiles to stand-alone Windows binaries
  • the binaries contain bytecode and a launcher
  • looks discontinued now (February 2014) – last change in 2006
  • 编译为独立的 Windows 二进制文件
  • 二进制文件包含字节码和启动器
  • 外观现已停产(2014 年 2 月)——最后一次更改是在 2006 年

BinaryPHP

二进制PHP

  • compiles to C++
  • looks discontinued now (February 2014) – last change in 2003
  • 编译为 C++
  • 外观现已停产(2014 年 2 月)——最后一次更改是在 2003 年

回答by zombat

The short answer is "no".

最简洁的答案是不”。

The current implementation of PHP is that of an interpreted language. You can argue the theoretical aspects of the fact that any language can technically be interpreted or compiled, but as it stands, the current implementations are such that PHP code requires an interpreter to run, and the interpreter manages the executing environment.

PHP 的当前实现是解释性语言的实现。您可以争论任何语言在技术上都可以被解释或编译这一事实的理论方面,但就目前而言,当前的实现是 PHP 代码需要解释器才能运行,而解释器管理执行环境。

To answer your question about uploading pre-compiled PHP bytecode, it's probably possible, but you'd have to implement a way for the PHP interpreter to read in such a file and work with it. With existing opcode caches out there already, it doesn't seem like a task that would reap much reward.

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

回答by Alex C

Since the question was first asked, there has been a change to that answer from a flat out "no" to a "kind of"

自从第一次问这个问题以来,这个答案已经从完全的“不”变成了“有点”

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

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

Hip Hop for PHP was a compiler that took PHP code and turned it into highly optimized C++ Apparently, some functions are not supported (for example 'explode')

PHP 的 Hip Hop 是一个编译器,它采用 PHP 代码并将其转换为高度优化的 C++ 显然,某些功能不受支持(例如“爆炸”)

I found this question while looking for more information on how to implement HipHop and thought I'd speak up :)

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

Since 2013 Facebook no longer use it, however, and it has been discontinued in favour of HHVM, which is not a compiler: https://en.wikipedia.org/wiki/HipHop_for_PHP

然而,自 2013 年以来 Facebook 不再使用它,并且它已被停止支持 HHVM,它不是一个编译器:https: //en.wikipedia.org/wiki/HipHop_for_PHP

回答by Gordon

There is also

还有

which aims

其目的

  • To encode entire script in a proprietary PHP application
  • To encode some classes and/or functions in a proprietary PHP application
  • To enable the production of php-gtk applications that could be used on client desktops, without the need for a php.exe.
  • To do the feasibility study for a PHP to C converter
  • 在专有 PHP 应用程序中对整个脚本进行编码
  • 在专有 PHP 应用程序中编码某些类和/或函数
  • 支持生成可在客户端桌面上使用的 php-gtk 应用程序,而无需 php.exe。
  • 为 PHP to C 转换器做可行性研究

The extension is available from PECL.

该扩展可从 PECL 获得。

回答by Paul Biggar

phcallows you to compile PHP programs into shared libraries, which can be uploaded to the server. The PHP program is compiled into binaries. It's done in such a way as to support evals, includes, and the entire PHP standard library.

phc允许你将 PHP 程序编译成共享库,可以上传到服务器。PHP 程序被编译成二进制文件。它以支持evals、includes 和整个 PHP 标准库的方式完成。

回答by asiby

If you are simply looking for producing a binary executable from a PHP script, then please avoid trying to make your question extremely precise because it will make it appear that you know exactly what you need. Besides, most PHP developer have absolutely zero clue about what a bytecode is.

如果您只是想从 PHP 脚本生成二进制可执行文件,那么请避免尝试使您的问题非常精确,因为它会使您看起来完全知道您需要什么。此外,大多数 PHP 开发人员对字节码是什么一无所知。

With that said, the answers is YES. I have just finished compiling a PHP script into a binary. And not just any binary. I have used the CDE application(link to Wayback Machine, the original linkis now broken) to turn it into an portable binary that can be distributed with all the dependencies and executed without any issue… and it works beautifully.

话虽如此,答案是肯定的。我刚刚完成将 PHP 脚本编译为二进制文件。而不仅仅是任何二进制文件。我使用了CDE 应用程序(指向 Wayback Machine 的链接原始链接现已断开)将其转换为一个可移植的二进制文件,该二进制文件可以随所有依赖项一起分发,并且可以毫无问题地执行……而且它运行良好。

All you need is to use phc.

您只需要使用phc

回答by John Dillick

Um, anybody heard of Zend Guard, which does exactly what this person is asking. It encodes/obfuscates PHP code into "machine code".

嗯,任何人都听说过Zend Guard,它完全符合这个人的要求。它将 PHP 代码编码/混淆为“机器代码”。

回答by Jakub Mí?ek

There are several "compilers" of PHP code. Most of them do not support all of PHP features, since these simply must be interpreted during run time.

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

We are using Phalanger - http://www.php-compiler.net/- that is supporting even those dirty PHP dynamic features, and still is able to compile them as .NET assembly, that can be distributed as a standalone DLL.

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

回答by cplusplus

see 5.5.x with the integrated OPcache module, volatile in a shared memory, much more performance and the dynamism principle of php remain untouched.

见 5.5.x 集成 OPcache 模块,在共享内存中不稳定,性能和 php 的动态原则保持不变。

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

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

回答by caiofior

In php 7 there is the php ini option opcache.file_cachethat saves the bytecode in a specific folder. In could be useful to in php cli script that are "compiled" and saved in a specific folder for a optimized reuse.

在 php 7 中有 php ini 选项opcache.file_cache将字节码保存在特定文件夹中。In 可能对“编译”并保存在特定文件夹中以优化重用的 php cli 脚本很有用。

Opcache it is not compiling but is something similar.

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