将 PHP 转换为 C++ 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1090124/
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 00:55:12  来源:igfitidea点击:

Convert PHP to C++ code

phpc++code-translation

提问by Viet

I'm looking for a way to convert PHP code to C++. There are a few reasons I want to do so:

我正在寻找一种将 PHP 代码转换为 C++ 的方法。我想这样做有几个原因:

Main reason: There are bunch of great PHP tools/software that I'd love to use and incorporate into C++ GUI or non-GUI applications

主要原因:有很多很棒的 PHP 工具/软件,我很想使用它们并将它们整合到 C++ GUI 或非 GUI 应用程序中

  1. To boost performance
  2. To avoid dependency on PHP libraries
  3. To avoid disclosing source code on shared hosting environments
  4. To be empowered by C++ language features and make use of frameworks like Qt C++ and Poco
  1. 提高性能
  2. 避免对 PHP 库的依赖
  3. 避免在共享托管环境中泄露源代码
  4. 获得 C++ 语言特性的支持,并使用 Qt C++ 和 Poco 等框架

So far I've found:

到目前为止,我发现:

http://sourceforge.net/projects/binaryphp

http://sourceforge.net/projects/binaryphp

http://www.mibsoftware.com/php2cpp/

http://www.mibsoftware.com/php2cpp/

Please share your ideas and tools that you know. Thank you in advance!

请分享您的想法和您知道的工具。先感谢您!

EDIT:I also need cross-platform operability and good match for $$, syntactic sugar functions and Interface.

编辑:我还需要跨平台的可操作性和 $$、语法糖函数和接口的良好匹配。

EDIT:Please note that 2 aforementioned tools use 2 different approaches. The former has built-in libraries to convert PHP procedural code and make it run. The latter translates (word by word) from PHP to C++.

编辑:请注意,上述 2 个工具使用 2 种不同的方法。前者具有内置库来转换 PHP 程序代码并使其运行。后者将(逐字)从 PHP 翻译成 C++。

采纳答案by naugtur

This might also get interesting in a short while:

这也可能会在短时间内变得有趣:

https://developers.facebook.com/blog/post/2010/02/02/hiphop-for-php--move-fast/

https://developers.facebook.com/blog/post/2010/02/02/hiphop-for-php--move-fast/

It's what keeps facebook alive ;) A technology that translates & compiles php to C++ and is going to go open-source. I expect it to be really well tested.

这就是让 facebook 保持活力的原因;) 一种将 php 转换和编译为 C++ 的技术,并将开源。我希望它能够得到很好的测试。

回答by Bob Somers

Maybe I don't fully grok the context of why you think you need to do this, but I don't think the reasons you posted are particularly compelling.

也许我没有完全理解为什么你认为你需要这样做的背景,但我认为你发布的原因并不是特别引人注目。

  1. Not very many web apps are CPU-bound, so I don't think you'll be impressed by the boost in performance. Compiling it down to native code isn't going to make your I/O or DB waits any shorter. There are better ways to scale a web app than moving to C++, and most of them involve reducing the time your script is waiting for data from the DB or disk (such as sharding your DB or moving data into a RAM cache).

  2. Are the library dependencies really that much of an issue? Many of the most popular libraries are bundled with the PHP core now, plus any additional ones are generally a cinch to install. Once you've got 'em, you don't touch 'em, so I'm not sure why this is a particularly compelling reason to move to C++.

  3. This is actually a very BAD reason to move to C++. Most shared hosting environments will let you run scripts in a PHP environment, but very few to none (unless you're looking at a VPS) will let you run an arbitrary binary like you'll have with a C++ app. There are better ways to control source readability in shared environments, like using file permissions correctly. If you're worried about clients stealing your PHP code, well, too bad. You can try to obfuscate it all you want, but it's generally pointless at the end of the day.

  4. What specific C++ features do you so desperately need that PHP does not offer? To be honest, the idea of your average PHP coder mucking about with pointers and memory allocation scares the sh*t out of me. There are very good frameworks available for PHP, and I'd suggest you completely exhaust all of those options before you turn to changing the language of your entire code base just for some features you like in particular framework.

  5. You mentioned cross-platform compatibility in your comment... PHP is honestly going to make this easier than C++ is. The PHP implementation is relatively standardized across platforms (except for a few lower-level functions) whereas you are likely going to be using a different compiler and system calls in C++.

  1. 没有多少 Web 应用程序受 CPU 限制,因此我认为性能的提升不会给您留下深刻印象。将其编译为本机代码不会使您的 I/O 或 DB 等待时间更短。有比迁移到 C++ 更好的扩展 Web 应用程序的方法,其中大多数涉及减少脚本等待来自数据库或磁盘的数据的时间(例如将数据库分片或将数据移动到 RAM 缓存中)。

  2. 库依赖真的有那么大的问题吗?许多最流行的库现在都与 PHP 核心捆绑在一起,而且任何额外的库通常都可以轻松安装。一旦你有了它们,你就不会碰它们,所以我不确定为什么这是转向 C++ 的一个特别令人信服的理由。

  3. 这实际上是转向 C++ 的一个非常糟糕的理由。大多数共享托管环境都可以让您在 PHP 环境中运行脚本,但很少有(除非您正在查看 VPS)可以让您像使用 C++ 应用程序一样运行任意二进制文件。有更好的方法来控制共享环境中的源代码可读性,例如正确使用文件权限。如果您担心客户端窃取您的 PHP 代码,那太糟糕了。您可以尝试对它进行任何您想要的混淆,但在一天结束时它通常毫无意义。

  4. 您非常需要哪些特定的 C++ 特性,而 PHP 没有提供?老实说,你的普通 PHP 程序员在指针和内存分配上胡思乱想的想法吓坏了我。有非常好的 PHP 框架可用,我建议您在为特定框架中您喜欢的某些功能而更改整个代码库的语言之前,完全用尽所有这些选项。

  5. 您在评论中提到了跨平台兼容性......老实说,PHP 会让这比 C++ 更容易。PHP 实现跨平台相对标准化(除了一些较低级别的函数),而您可能会在 C++ 中使用不同的编译器和系统调用。

回答by bslima

Facebook hiphop is open for a while now: https://github.com/facebook/hiphop-php/

Facebook hiphop 现在开放了一段时间:https: //github.com/facebook/hiphop-php/

回答by Viet

After much research, I've found http://www.phpcompiler.org/doc/maketea.pdfand http://rosecompiler.org/ROSE_Tutorial/ROSE-0.9.4a-Tutorial.pdf. Since PHP source code can be parsed and then traslated to XML, converting it to C++ is just a matter of XSLT. Moreover, PHPCompiler also supports plugins and various transformations of source code (based on AST transformations). I think that should be sufficient for now.

经过大量研究,我找到了http://www.phpcompiler.org/doc/maketea.pdfhttp://rosecompiler.org/ROSE_Tutorial/ROSE-0.9.4a-Tutorial.pdf。由于可以解析 PHP 源代码,然后将其转换为 XML,因此将其转换为 C++ 只是 XSLT 的问题。此外,PHPCompiler 还支持插件和各种源代码转换(基于 AST 转换)。我认为现在应该足够了。

Some extra references:

一些额外的参考:

  1. http://www.phpcompiler.org
  2. http://www.phpcompiler.org/doc/latest/devintro.html
  3. http://rosecompiler.org
  4. http://rosecompiler.org/ROSE_UserManual/ROSE-UserManual.pdf
  1. http://www.phpcompiler.org
  2. http://www.phpcompiler.org/doc/latest/devintro.html
  3. http://rosecompiler.org
  4. http://rosecompiler.org/ROSE_UserManual/ROSE-UserManual.pdf

Thanks everyone for participating in the discussion!

感谢大家参与讨论!

回答by Nick Berardi

1-3 can be accomplished by the Zend Compiler

1-3 可以通过 Zend Compiler 来完成

http://www.zend.com/en/

http://www.zend.com/en/

4 is a little weird, because isn't the Qt C++ a desktop UI framework?

4 有点奇怪,因为 Qt C++ 不是桌面 UI 框架吗?