首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 擴充開發基礎(二):config.m4、zend_module_entry 結構與編譯流程

     編輯:PHP 相關知識

三個核心檔案

  • config.m4:Unix 建構系統設定 (Windows 為 config.w32)
  • php_extname.h:擴充標頭檔,宣告結構體、全域變數
  • extname.c:主程式,實作 MINIT/RINIT/MSHUTDOWN/RSHUTDOWN/MINFO 等鉤子

config.m4 關鍵設定

# 啟用擴充 (無外部依賴用 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_module_entry 結構解析

// 定義於 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)

編譯安裝流程

  1. phpize (生成 configure、Makefile.in)
  2. ./configure --enable-hello
  3. make
  4. make test (可選)
  5. sudo make install
  6. 編輯 php.ini 加入 extension=hello.so
  7. php -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 或連結順序錯誤
  • Windows 編譯:需 Visual Studio、PHP SDK、config.w32 而非 config.m4
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved