views:

4021

answers:

4

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

+16  A: 

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

Bob Somers
With 5.3.0 released many of the lower level functions that I think you are talking about have been interfaced with Windows, in the most recent VC9 compiled builds, where they strip out the UNIX to Windows translation layer and call the Windows interfaces directly.
Nick Berardi
Thanks Bob. You gave very good arguments. I have my own reasons to pursue this before making this decision. On cross-platform compatibility, Qt C++ and Poco C++ do great job and I love them.
Viet
Thank you Nick for the note.
Viet
+3  A: 

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?

Nick Berardi
Thanks Nick :) I need C++ code to work and improve so I'll continue seeking.
Viet
Qt C++ can be used to write console applications and daemons too :) I also tried to write CGI application using Qt to enjoy its breadth and depth of libraries (networking, string processing, I/O ...)
Viet
A: 

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!

Viet
+4  A: 

This might also get interesting in a short while:

http://developers.facebook.com/news.php?blog=1&story=358

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.

naugtur
The timing of this question was interesting, as that just got announced.
MaxVT
well.. the question's quite old, as You might have overlooked:) but I figured it's worth putting here.
naugtur
+1. That's really interesting man! Browsing the pages and watching the clips now!
Viet
I started small project by myself to do translation but thanks god, now we have HipHop!
Viet
The GitHub repo for HipHop is here: http://github.com/facebook/hiphop-php/
Matt J