config.m4:Unix 建構系統設定 (Windows 為 config.w32)php_extname.h:擴充標頭檔,宣告結構體、全域變數extname.c:主程式,實作 MINIT/RINIT/MSHUTDOWN/RSHUTDOWN/MINFO 等鉤子# 啟用擴充 (無外部依賴用 PHP_ARG_ENABLE)
PHP_ARG_ENABLE(extname, whether to enable extname support,
[ --enable-extname Enable extname support])
if test "$PHP_EXTNAME" != "no"; then
# 定義擴充原始檔
PHP_NEW_EXTENSION(extname, extname.c, $ext_shared)
fi
若有外部函式庫:
PHP_ARG_WITH(extname, for extname support,
[ --with-extname=DIR Include extname support])
PHP_ADD_INCLUDE($EXTNAME_DIR/include)
PHP_CHECK_LIBRARY(extname, extname_function,
[ PHP_ADD_LIBRARY_WITH_PATH(extname, $EXTNAME_DIR/lib, EXTNAME_SHARED_LIBADD)
AC_DEFINE(HAVE_EXTNAME_LIB, 1, [ ])
],[
AC_MSG_ERROR([wrong extname lib version or lib not found])
],[
-L$EXTNAME_DIR/lib -lm
])
PHP_SUBST(EXTNAME_SHARED_LIBADD)
PHP_NEW_EXTENSION(extname, extname.c, $ext_shared)
// 定義於 Zend/zend_modules.h struct _zend_module_entry { unsigned short size; unsigned int zend_api; unsigned char zend_debug; unsigned char zts; const struct _zend_ini_entry *ini_entry; const struct _zend_module_dep *deps; const char *name; // 擴充名稱 const struct _zend_function_entry *functions; // 函式表 int (*module_startup_func)(INIT_FUNC_ARGS); // MINIT int (*module_shutdown_func)(SHUTDOWN_FUNC_ARGS); // MSHUTDOWN int (*request_startup_func)(INIT_FUNC_ARGS); // RINIT int (*request_shutdown_func)(SHUTDOWN_FUNC_ARGS); // RSHUTDOWN void (*info_func)(ZEND_MODULE_INFO_FUNC_ARGS); // MINFO (phpinfo) const char *version; size_t globals_size; // ... ZTS 相關欄位 };
// php_hello.h
#ifndef PHP_HELLO_H
#define PHP_HELLO_H
extern zend_module_entry hello_module_entry;
#define phpext_hello_ptr &hello_module_entry
#endif
// hello.c
#include "php_hello.h"
#include "php.h"
ZEND_FUNCTION(hello_world) {
php_printf("Hello World!\n");
}
const zend_function_entry hello_functions[] = {
ZEND_FE(hello_world, NULL)
ZEND_FE_END
};
zend_module_entry hello_module_entry = {
STANDARD_MODULE_HEADER,
"hello",
hello_functions,
NULL, // MINIT
NULL, // MSHUTDOWN
NULL, // RINIT
NULL, // RSHUTDOWN
NULL, // MINFO
"1.0",
STANDARD_MODULE_PROPERTIES
};
ZEND_GET_MODULE(hello)
phpize (生成 configure、Makefile.in)./configure --enable-hellomakemake test (可選)sudo make installphp.ini 加入 extension=hello.sophp -m | grep hello 驗證phpize 找不到:安裝 php-dev/php-devel 套件ext/standard/php_smart_str.h: No such file:PHP 版本不匹配,需對應版本原始碼undefined symbol:config.m4 缺少 PHP_ADD_LIBRARY 或連結順序錯誤config.w32 而非 config.m4