Question

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

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

  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

So far I've found:

http://sourceforge.net/projects/binaryphp

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++.

Was it helpful?

Solution

This might also get interesting in a short while:

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.

OTHER TIPS

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++.

After much research, I've found http://www.phpcompiler.org/doc/maketea.pdf and 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.

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

Thanks everyone for participating in the discussion!

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

1-3 can be accomplished by the Zend Compiler

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top