Does PHP have a virtual machine like Java?

2019-02-12 09:25发布

问题:

Does PHP have a virtual machine like Java?

回答1:

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



回答2:

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.



回答3:

Yes, but only recently it is quite like the JVM for Java. It was invented by facebook and announced at the OSCON conference the other week (July 2013).

A news report on the new PHP VM can be read here, and the code and more info can be found on github.