Some summaries of __halt_compiler

WBOY
Release: 2016-08-08 09:22:33
Original
1506 people have browsed it
  • __halt_compiler(), as the name suggests, is a function that tells the compiler to stop compilation. When the compiler executes this point, it will no longer parse the following parts. It should be noted that this function needs to be used directly in the outermost layer of the php file and cannot be used within a function.

According to the introduction in the PHP manual, this function is commonly used to embed data in scripts, similar to installation files. That is to say, after __halt_compiler();, put some files that do not need to be compiled, such as binary noise (clutter), compressed files and other types of files. Such as the following code:

// 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.)
Copy after login
TIP: __COMPILER_HALT_OFFSET__ constant is used to get the beginning of the data bytes. Requires __halt_compiler() to be used.
Next, let’s talk about a specific example of a php installation file:

Before the introduction of __halt_compiler() in php5.1, files compressed using gzdeflat() often contained files that could not be parsed by the php interpreter (parser ) to read the special ASCII code, causing an error. In order to prevent errors, base64_encode() is used to encode the data generated by gzdeflate(), which will increase the file size by approximately 33%. This is a waste of memory.

$packed = base64_encode(gzdeflate('the old package'));
//unpacked
$unpacked = base64_decode(gzinflate($packed));
Copy after login

With __halt_compiler(), we can no longer use base64_encode() to encode, but directly put the data after __halt_compiler(), so that it will not be compiled, resulting in Wrong.
// 打开脚本自身文件
$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!!!
Copy after login

The above introduces some summaries of __halt_compiler, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template