OPCODE是PHP编译后的二进制代码,生成的Opcode做为一种中间语言,能够帮助实现PHP源程序代码的不开源,固然,这种代码也很容易被反编译,不过对于一些简单的场景也是很足够了。
编译的基本思路是首先在php.ini中配置加载opcache扩展,并配置相关参数,而后执行一个PHP脚本遍历源代码目录,并进行编译,核心的函数是opcache_compile_file(),该函数会根据php.ini中的参数,编译并输出二进制代码。php
首先,配置PHP.INI文件中的opcache相关参数,以开启OPCACHE功能:web
zend_extension=php_opcache.dll [opcache] ; Determines if Zend OPCache is enabled opcache.enable=1 ; Determines if Zend OPCache is enabled for the CLI version of PHP opcache.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 200 and 100000 are 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 are 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 is 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
从网上获得一段编译的代码,本身根据实际状况修改了一下,代码以下:服务器
<?php /** * Created by PhpStorm. * User: Lancelot * Date: 2018-02-09 * Time: 14:04 */ $dir = $argv[1]; opcache_compile_files($dir); function opcache_compile_files($dir) { $cacheMd5 = 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\\' . $cacheMd5 . '\\' . $append; $cachePath = str_replace($search, $repl, $phpFile) . '.bin'; if (file_exists($cachePath)) { echo "{$phpFile}\n"; file_put_contents($phpFile, ''); //清空原来的PHP脚本 } } } } } }
上面代码的思路是遍历传入的源文件目录,对找到的PHP文件进行编译,而后检查输出路径中是否有已经编译成功的文件,若是有,则把源PHP文件内容清空,这样服务器就会直接调用编译后的代码,而不是从新编译了。app
使用方法(假设上面代码的php文件名为opcache_compile_file.php),第一个参数的值为待编译的PHP源代码目录:框架
php opcache_compile_file.php "C:\BeyondScreen\www\byserver"
在实现上面的PHP代码编译过程当中,遇到了一些问题,以下:
一、 源目录中的文件没有所有编译;
咱们的框架是Yii2,编译后发现框架的不少代码没有编译,没有时间去查找缘由,只是在编译脚本中增长了一些逻辑,只对内容不为空的PHP文件进行编译,而后编译时执行两遍编译命令来确保所有PHP文件都被编译。
二、 部分PHP文件编译失败;
大多数问题是XXX类已经存在,因此编译失败,处理方案是编译脚本中检查编译结果,对于编译失败的PHP文件不清空。ide