views:

1041

answers:

5

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.

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

+1  A: 

PHP doesn't really get compiled as with many programs. You can use Zend's encoder to make it unreadable though.

Joe
doesn't it get compiled in the same way that perl gets compiled?
Carson Myers
PHP is compiled on the server on the fly I believe. I have used an encoder to hide the source code though. If that is what you are worried about. They work really well.
Joe
nah, I was just interested in the theoretical aspect of it
Carson Myers
+7  A: 

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.

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.

zombat
Its often the case that you do need an interpreter to properly run PHP programs, but that doesn't mean a compiler can't provide it in the compiled code. phc (phpcompiler.org) handles all the problems you describe. To the best of my knowledge, Roadsend (roadsend.com) does too.
Paul Biggar
+6  A: 

Googling "PHP compiler" turns up a number of 3rd party solutions:

http://www.phpcompiler.org

http://www.roadsend.com/home/index.php?pageID=compiler

http://www.bambalam.se/bamcompile/

http://sourceforge.net/projects/binaryphp/

Frank Farmer
http://www.codeplex.com/Phalanger (compiler for .NET)
jrummell
bamcompile worked easiest for me, on winxp..
aland
+3  A: 

phc allows 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.

Paul Biggar
+1  A: 

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

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

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

Alex C
Thanks for the addition :)
Carson Myers