啟動, 終止, 以及其中的一些點
在本書中, 你已經多次使用MINIT函數在php加載你擴展的共享庫時執行初始化任務. 在第1章"php的生命周期"中, 你還學習了其他三個啟動/終止函數, 與MINIT對應的是MSHUTDOWN, 另外還有一對RINIT/RSHUTDOWN方法在每個頁面請求啟動和終止時被調用.
生命周期
除了這四個直接鏈接到模塊結構的函數外, 還有兩個函數僅用於線程環境, 用來處理每個線程的啟動和終止, 以及它們使用的似有存儲空間.開始之前, 首先將你的php擴展骨架程序拷貝到php源碼樹的ext/sample4下. 代碼如下
config.m4
PHP_ARG_ENABLE(sample4,
[Whether to enable the "sample4" extension],
[ enable-sample4 Enable "sample4" extension support])
if test $PHP_SAMPLE4 != "no"; then
PHP_SUBST(SAMPLE4_SHARED_LIBADD)
PHP_NEW_EXTENSION(sample4, sample4.c, $ext_shared)
fi
php_sample4.h
#ifndef PHP_SAMPLE4_H
/* Prevent double inclusion */
#define PHP_SAMPLE4_H
/* Define Extension Properties */
#define PHP_SAMPLE4_EXTNAME "sample4"
#define PHP_SAMPLE4_EXTVER "1.0"
/* Import configure options
when building outside of
the PHP source tree */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* Include PHP Standard Header */
#include "php.h"
/* Define the entry point symbol
* Zend will use when loading this module
*/
extern zend_module_entry sample4_module_entry;
#define phpext_sample4_ptr &sample4_module_entry
#endif /* PHP_SAMPLE4_H */
sample4.c
#include "php_sample4.h"
#include "ext/standard/info.h"
static function_entry php_sample4_functions[] = {
{ NULL, NULL, NULL }
};
PHP_MINIT_FUNCTION(sample4)
{
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(sample4)
{
return SUCCESS;
}
PHP_RINIT_FUNCTION(sample4)
{
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(sample4)
{
return SUCCESS;
}
PHP_MINFO_FUNCTION(sample4)
{
}
zend_module_entry sample4_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_SAMPLE4_EXTNAME,
php_sample4_functions,
PHP_MINIT(sample4),
PHP_MSHUTDOWN(sample4),
PHP_RINIT(sample4),
PHP_RSHUTDOWN(sample4),
PHP_MINFO(sample4),
#if ZEND_MODULE_API_NO >= 20010901
PHP_SAMPLE4_EXTVER,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_SAMPLE4
ZEND_GET_MODULE(sample4)
#endif
要注意, 每個啟動和終止函數在退出時都返回SUCCESS. 如果這些函數中某個返回FAILURE, 引擎就認為這個過程失敗並中斷php的執行.