Heim  >  Artikel  >  Backend-Entwicklung  >  __halt_compiler的一些总结

__halt_compiler的一些总结

WBOY
WBOYOriginal
2016-08-08 09:22:331458Durchsuche
  • __halt_compiler(),顾名思义,是让编译器停止编译的函数,当编译器执行到这之后就不再去解析(parsing)后面的部分了。需要注意的是,该函数需要在php文件的最外层直接使用,不能在函数里使用。

根据php手册上的介绍,该函数常用与在脚本内嵌入数据,类似于安装文件。也就是说在__halt_compiler();后面放一些不需要编译的如:二进制噪音(clutter)、压缩文件等各种类型的文件。如以下代码:

// open this file
$fp = fopen(__FILE__, 'r');
// seek file pointer to data
fseek($fp, __COMPILER_HALT_OFFSET__);
// and output it
var_dump(stream_get_contents($fp));
// the end of the script execution
__halt_compiler(); the installation data (eg. tar, gz, PHP, etc.)
TIP:__COMPILER_HALT_OFFSET__ 常量被用于获取数据字节的开始处。需要有__halt_compiler()才能使用。
  • 接下来,说一个具体的php安装文件的例子:

在php5.1引入__halt_compiler()之前,使用gzdeflat()压缩的文件因为经常含有不能被php 解释器(parser)读取的特殊ASCII码,从而发生错误。为了防止错误的发生就使用base64_encode()来编码gzdeflate()所产生的数据,而这样会造成文件体积增大约33%。这是一种对内存的浪费。

$packed = base64_encode(gzdeflate('the old package'));
//unpacked
$unpacked = base64_decode(gzinflate($packed));
而在有了__halt_compiler()之后,我们就可以不再用base64_encode()进行编码了,而是直接将数据放到__halt_compiler()之后,这样它就不会被编译,从而产生错误了。
// 打开脚本自身文件
$fp = fopen(__FILE__, 'rb');
// 找到数据在文件中的指针
//__COMPILER_HALT_OFFSET__ 将会返回
//__halt_compiler();之后的指针
fseek($fp, __COMPILER_HALT_OFFSET__);
// 输出文件
$unpacked = gzinflate(stream_get_contents($fp));
__halt_compiler();
//now here... all the binary gzdeflate already items!!!

以上就介绍了__halt_compiler的一些总结,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:session原理Nächster Artikel:纯手工玩转 Nginx 日志