Question
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?
Solution
After this question was asked, Facebook launched HipHop for PHP which 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.
Beyond that, googling PHP compiler
turns up a number of 3rd party solutions.
- compiles to native binaries
- not very active now (February 2014) – last version in 2011, last change in summer 2013
- GitHub, GitHub of a rewrite
- free, open source implementation of PHP with compiler
- compiles to native binaries (Windows, Linux)
- discontinued since 2010 till contributors found – website down, stays on GitHub where last change is from early 2012
- 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
- 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
- compiles to stand-alone Windows binaries
- the binaries contain bytecode and a launcher
- looks discontinued now (February 2014) – last change in 2006
- compiles to C++
- looks discontinued now (February 2014) – last change in 2003
OTHER TIPS
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.
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 was a compiler that took PHP code and turned 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 :)
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
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
The extension is available from PECL.
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 eval
s, include
s, and the entire PHP standard library.
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.
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 link is 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.
All you need is to use phc.
Um, anybody heard of Zend Guard, which does exactly what this person is asking. It encodes/obfuscates PHP code into "machine code".
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.
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.
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.
In php 7 there is the php ini option opcache.file_cache that 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.
Opcache it is not compiling but is something similar.
PHP doesn't really get compiled as with many programs. You can use Zend's encoder to make it unreadable though.