PHP code compiled to opcode

Source: Internet
Author: User
Tags php source code zend

Brief introduction

OpCode is a PHP compiled binary code, the generated opcode as an intermediate language, can help to implement the PHP source code is not open source, of course, this code is also easy to decompile, but for some simple scenes is enough.
The basic idea of compiling is to first configure the load Opcache extension in php.ini, and configure the relevant parameters, then execute a PHP script to traverse the source code directory and compile, the core function is Opcache_compile_file (), The function compiles and outputs the binary code according to the parameters in the php.ini.

Preparatory work

First, configure the Opcache-related parameters in the php.ini file to turn on the Opcache feature:

Zend_extension=php_opcache.dll[opcache]; Determines if Zend Opcache is enabledopcache.enable=1; Determines if Zend Opcache is enabled for the CLI version of Phpopcache.enable_cli=1; The amount of memory for interned strings in mbytes.opcache.interned_strings_buffer=8; The maximum number of keys (scripts) in the Opcache hash table.; Only numbers between and 100000 is allowed.opcache.max_accelerated_files=8000; When disabled, you must reset the Opcache manually or restart the; Webserver for changes to the filesystem to take effect.opcache.validate_timestamps=0; If disabled, all PHPDOC comments is dropped from the code to reduce the; Size of the optimized code.opcache.save_comments=0; Enables and sets the second level cache directory.; It should improve performance when SHM memory was full, at server restart or; SHM Reset. The default "" Disables file based Caching.opcache.file_cache=c:\myapp\www\cache; Enables or disables opcode caching in shared memory.opcache.file_cache_only=0
Compile

From the internet to get a compiled code, according to the actual situation modified, the code is as follows:

<?php/** * Created by Phpstorm. * User:lancelot * date:2018-02-09 * time:14:04 * * $dir = $argv [1];opcache_compile_files ($dir); function Opcache_compile_f    Iles ($dir) {$cacheMd 5 = file_get_contents ("CacheMd5.txt"); foreach (New Recursiveiteratoriterator (New Recursivedirectoryiterator ($dir)) as $v) {if (! $v->isdir () &&            Preg_match ('%\.php$% ', $v->getrealpath ())) {$phpFile = $v->getrealpath ();               if (filesize ($phpFile) > 2) {if (Opcache_compile_file ($phpFile)) {$search = $dir;                    $append = Str_replace (': ', ' ', $dir); $repl = ' C:\BeyondScreen\www\cache\\ '. $cacheMd 5. ‘\\‘ .                    $append; $cachePath = Str_replace ($search, $repl, $phpFile).                    '. bin ';                        if (file_exists ($cachePath)) {echo "{$phpFile}\n"; File_put_contents ($phpFile, "); Empty the original PHP script}}}        }    }} 

The idea of the above code is to traverse the incoming source file directory, compile the found PHP file, and then check the output path for the successful compilation of the file, if so, the source PHP file content is emptied, so the server will directly invoke the compiled code, rather than recompile.

Using the method (assuming the PHP file name of the above code is opcache_compile_file.php), the value of the first parameter is the PHP source code directory to be compiled:

php opcache_compile_file.php "C:\BeyondScreen\www\byserver"
Several questions

In the implementation of the above PHP code compilation process, encountered a number of problems, as follows:
1, the source directory of the files are not all compiled;
Our framework is YII2, compiled after the discovery framework of many of the code is not compiled, there is no time to find the reason, just add some logic in the compilation script, only the content is not empty PHP files compiled, and then run two times compile command to ensure that all PHP files are compiled.
2, some php file compilation failed;
Most of the problem is that the XXX class already exists, so the compilation fails, the processing scenario is the compilation script to check the compilation results, for the compilation failed PHP files are not emptied.

PHP code compiled to opcode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.