- URL: https://www.laruence.com/en/2012/04/01/2571.html
- Please include attribution when republishing.
Today's April Fools' Day, but this article title isn't me playing a joke on everyone. 🙂
First, as everyone knows, PHP is also a compiled scripting language. Unlike other pre-compiled languages, it's not compiled into intermediate code and then shipped... it needs to be compiled on every run..
For this reason, some Opcode Caches came into being, such as the open-source APC, eacc. And the commercial Zend O+.
So why doesn't PHP separate compilation from execution?
Although PHP is a compiled scripting language, its compilation is very fast. Its compilation does no semantic optimization at all — it simply and faithfully translates the code you wrote into the corresponding Opcodes. Other languages, because their compilers do a lot of optimization work, make compilation comparatively heavy, and to some extent that's what pushes them to separate compilation from execution.
So, in theory, trying to achieve source-code encryption through compile/execute separation won't yield much benefit, because it's very easy to reverse.
In addition, separating compilation and execution directly doesn't bring especially large benefits; on the contrary, it lowers the efficiency of debugging and deployment (think about it: modify, compile, release, see the effect), and Opcode Cache tools like APC are already quite mature..
Getting to here, please note this sentence: "its compilation does no semantic optimization at all"....
This is exactly why I say that PHP demands more from the programmer. Unlike other compiled languages, PHP doesn't do some optimizations for you at compile time. For example, for the following code:
$j = "laruence";
for ($i=0;$i<strlen($j);$i++) {
}
If it were another pre-compiled language, its compiler might optimize it for you, hoisting strlen to the front so it's only done once. For PHP, its compilation does no optimization at all, which means your strlen will be faithfully called 8 times.
Take another example:
$table = "table";
while($i++ < 1000) {
$sql = "select * from " . $table . " where id = " . $i;
}
That's right, "select * from " . $table gets concatenated 1000 times..
As you can see, a PHP programmer needs to think carefully about how your code will be executed, and how to write your code so that the final execution efficiency is highest. Unlike other languages, where a programmer can hand some of the optimization work over to the compiler.
This is why I say "PHP demands more from the programmer." Of course, whether that's a good thing or a bad one is a matter of perspective.
Be First to Comment