php代码编译_PHP代码编译为OPCODE

简介

OPCODE是PHP编译后的二进制代码,生成的Opcode作为一种中间语言,可以帮助实现PHP源程序代码的不开源,当然,这种代码也很容易被反编译,不过对于一些简单的场景也是很足够了。

编译的基本思路是首先在php.ini中配置加载opcache扩展,并配置相关参数,然后执行一个PHP脚本遍历源代码目录,并进行编译,核心的函数是opcache_compile_file(),该函数会根据php.ini中的参数,编译并输出二进制代码。

准备工作

首先,配置PHP.INI文件中的opcache相关参数,以开启OPCACHE功能:

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

编译

从网上得到一段编译的代码,自己根据实际情况修改了一下,代码如下:

/**

* 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文件内容清空,这样服务器就会直接调用编译后的代码,而不是重新编译了。

使用方法(假设上面代码的php文件名为opcache_compile_file.php),第一个参数的值为待编译的PHP源代码目录:

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

几个问题

在实现上面的PHP代码编译过程中,遇到了一些问题,如下:

1、 源目录中的文件没有全部编译;

我们的框架是Yii2,编译后发现框架的很多代码没有编译,没有时间去查找原因,只是在编译脚本中增加了一些逻辑,只对内容不为空的PHP文件进行编译,然后编译时执行两遍编译命令来确保全部PHP文件都被编译。

2、 部分PHP文件编译失败;

大多数问题是XXX类已经存在,所以编译失败,处理方案是编译脚本中检查编译结果,对于编译失败的PHP文件不清空。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值