Does PHP have a virtual machine like Java?
+1
A:
Yes.
Independently from the platform PHP is running on, the scripts are compiled into the same bytecode and run by the Zend Engine.
The difference from Java is that this compiled code is usually not stored into separate files and the scripts are re-compiled on each execution (however, see opcode caches).
Artefacto
2010-08-01 03:08:44
Are their conceptual differences between the Zend Engine and the JVM?
Christopher Altman
2010-08-01 03:09:59
@Chris That question is very vague and I don't enough of JVM implementations to even sketch an answer.
Artefacto
2010-08-01 03:11:21
That is fair. Thank you.
Christopher Altman
2010-08-01 03:12:38
@Chris You can try to tag this question java to get some attention.
Artefacto
2010-08-01 03:13:10
You don't know the JVM too much, but do you know a little about the Zend Engine? Like, if it recompiles Zend Engine bytecode to native bytecode? This is potentially one conceptual difference.
zneak
2010-08-01 03:29:31
The major difference is that the JVM deals in byte code from the start after a compilation step, while PHP deals with source code.
EJP
2010-08-01 04:07:46
@EJF That difference is not that great once you start adding opcode caches, but we can point one difference: compilation is not all done before the execution of the script, `include` directives, for instance, may trigger compilation after execution has started.
Artefacto
2010-08-01 05:07:15
@zneak - what is *"native bytecode"*? It sound like you are getting your terminology confused here ...
Stephen C
2010-08-01 05:08:51
@EJG Yes, we can't point the lack of JIT compilation. I'd also add lack of multi-threading support, security manager, ... I suppose we could be here for quite a while enumerating things PHP doesn't have :p
Artefacto
2010-08-01 05:08:54
@Stephen C: _native bytecode_ would be _machine code_.
zneak
2010-08-01 14:53:19
@zneak - well that's what I thought you meant. But I don't think you'll find that anyone else refers to machine code / native code as "native bytecode". Bytecode is called **byte** code for a reason ... and that reason does not apply to typical native instruction sets.
Stephen C
2010-08-01 15:04:44
Where it says 'can't point', 'can point' should be read.
Artefacto
2010-08-01 23:45:06
A:
Another important difference between the Zend Engine and a typical JVM is in the way they execute bytecodes:
- The Zend Engine executes (interprets) the compiled bytecodes directly. (At least that's what I think happens. I wasn't able to confirm this from the Zend online documentation!)
- A JVM will normally use a JIT compiler to compile bytecodes to native instructions, and then execute the native instructions.
Actually, JVM behaviour is more complicated than this. JVMs don't always compile to native code, and when they do, they typically delay JIT compilation until they figure it is worth doing. Prior to that, they execute the bytecodes directly.
Stephen C
2010-08-01 05:07:12
Looking at the source code for Zend shows that it indeed interprets the bytecode directly, rather than converting it to native. And the code isn't pretty either...
siride
2010-08-01 06:13:56
I had many "what the bleep"-moments when stepping through php source code but frankly the vm elements of the zend engine aren't among those. In comparison to other languages ...well, most of them are like this.
VolkerK
2010-08-01 06:47:00