Home > php教程 > php手册 > body text

php 变量生命周期:PHP源码分析-PHP的生命周期

WBOY
Release: 2016-06-21 08:50:35
Original
1674 people have browsed it

PHP的最多的两种运行模式是WEB模式、CLI模式。
无论哪种模式,PHP工作原理都是一样的,作为一种SAPI运行。
1、当我们在终端敲入php这个命令的时候,它使用的是CLI。
它就像一个web服务器一样来支持php完成这个请求,请求完成后再重新把控制权交给终端。
2、当使用Apache作为宿主时,当一个请求到来时,PHP会来支持完成这个请求。
main/php.h中定义了以下几个宏
#define PHP_MINIT_FUNCTION ZEND_MODULE_STARTUP_D
#define PHP_MSHUTDOWN_FUNCTION ZEND_MODULE_SHUTDOWN_D
#define PHP_RINIT_FUNCTION ZEND_MODULE_ACTIVATE_D
#define PHP_RSHUTDOWN_FUNCTION ZEND_MODULE_DEACTIVATE_D
#define PHP_MINFO_FUNCTION ZEND_MODULE_INFO_D
#define PHP_GINIT_FUNCTION ZEND_GINIT_FUNCTION
#define PHP_GSHUTDOWN_FUNCTION ZEND_GSHUTDOWN_FUNCTION
对应的作用是
PHP_MINIT_FUNCTION 初始化module时运行
PHP_MSHUTDOWN_FUNCTION 当module被卸载时运行
PHP_RINIT_FUNCTION 当一个REQUEST请求初始化时运行
PHP_RSHUTDOWN_FUNCTION 当一个REQUEST请求结束时运行
PHP_MINFO_FUNCTION 这个是设置phpinfo中这个模块的信息
PHP_GINIT_FUNCTION 初始化全局变量时
PHP_GSHUTDOWN_FUNCTION 释放全局变量时
看一个自定义扩展案例片段:
int minit_time;
PHP_MINIT_FUNCTION(test)
{
minit_time = time(NULL);
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(test)
{
FILE *fp=fopen("mshutdown.txt","a+");
fprintf(fp,"%ld\n",time(NULL));//让我们看看是不是每次请求结束都会在这个文件里追加数据
fclose(fp);
return SUCCESS;
}
int rinit_time;
PHP_RINIT_FUNCTION(test)
{
rinit_time = time(NULL);
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(test)
{
FILE *fp=fopen("rshutdown.txt","a+");
fprintf(fp,"%ld\n",time(NULL));//让我们看看是不是每次请求结束都会在这个文件里追加数据
fclose(fp);
return SUCCESS;
}
PHP_MINFO_FUNCTION(test)
{
php_info_print_table_start();//调用php_write输出HTML标签
php_info_print_table_header(2, "module info", "enabled"); 本文链接http://www.cxybl.com/html/wlbc/Php/20121221/35071.html



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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!