Regarding the separation of compilation and execution of & rdquo; PHP, some people have been asking this question, and some people have been trying it. the author believes that after compilation and execution separation, the performance can be improved and code protection can be implemented. I am not quite familiar with this feature, because there is an input-output ratio. let me explain it to you. However, in the end, I will provide you with a solution to achieve this.
Some people have been asking about the issue of separating PHP compilation and execution. the author believes that after compilation and execution separation, the performance can be improved and code protection can be implemented.
I am not quite familiar with this feature, because there is an input-output ratio. let me explain it to you. However, in the end, I will provide you with a solution to implement this function.
1. PHP compilation is not very time-consuming
I have introduced in my previous articles that PHP compilation is a linear compilation process without optimization, so this process is very fast. the author of the feature of separation of compilation and execution thinks that after separation, the compilation process can be saved, which will greatly improve the performance.
Note: Some people have suggested that after compilation and execution are separated, various optimizations can be made during compilation, thus affecting the execution speed. this is indeed a research direction.
2. development speed
One of the advantages of PHP is that it is very convenient to develop, deploy, and debug, and changes take effect immediately. in this case, you will feel more deeply when fixing online bugs in a 100,000 rush ,? If we adopt the compilation/execution separation, the changes need to be compiled first and then deployed before they can take effect. this is not a good thing for development.
3. we have third-party code caching tools such as APC/Zend O +.
Third-party code caching tools (Opcodes Cache) such as APC are relatively mature and transparent to developers. you only need to install APC on the server, the compilation/execution splitting performance can be improved.
4. simple compilation/execution separation does not effectively implement code protection
The reason is very simple. PHP compilation is not optimized, so it is easy to be decompiled. of course, I do not deny that using binary content does play a role.
In addition, there are some factors, such as the compilation/execution separation scheme which is being done by some people, but not yet mature.
Finally, we can do the same now. here I will provide you with a similar solution:
To separate compilation and execution, we can use APC. APC provides a family of apc_bin_dump and apc_bin_load functions to export Opcodes cache to external files.
However, it is a pity that these functions have never been able to work properly before, and this is related to the fact that previous developers no longer focus on this because of time.
After I reorganized the apc_bin series of functions, after the fix, this part of the function now finally works properly (starting from the APC-3.1.12), so based on these functions, we can implement separation of compilation and execution.
The idea is very simple. locally export our php files to binfiles through apc_bin_dumpfile, and then read these binfiles through apc_bin_loadfile on the server. compile and execute separation. a simple sample code is as follows:
$ find ./ -name "*.php" -exec php -r "apc_bin_dumpfile(array('{}'), array(), '{}' . '.bin');" \;
Then the part of the file on the server is automatically loaded:
Of course, this is just a simple example. if you want to use it, you also need to consider the possibility that the cache will be swapped out. a solution is to set two auto-loading functions. The first one is as above, if the second file is called, it indicates that the cache is swapped out, and an empty file is included, so you can load the binfile again.
Of course, you can also package all the files into a binfile, load the file only once, and then hand it over to the APC Cache on the server. for those who want the "code protection" function, you can use APC to complete these tasks for free. of course, because it is a memory image dump, it is affected by the size of the PHP version and the system, but for general applications, this can be easily matched.
Note: this article