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

編寫第一個 PHP 擴展:從骨架到編譯載入完整流程

     編輯:PHP 綜合知識

PHP 擴展開發入門實戰:建立最小骨架(config.m4、php_sample.h、sample.c),理解 zend_module_entry 結構,編譯安裝並驗證載入。

一、目錄結構與檔案

php-src/ext/sample/
├── config.m4          # 建構配置
├── php_sample.h       # 頭文件:常數、入口宣告
└── sample.c           # 核心實作

二、config.m4 —— 建構系統整合

PHP_ARG_ENABLE(sample, [Whether to enable the "sample" extension],
    [  --enable-sample    Enable "sample" extension support])

if test "$PHP_SAMPLE" != "no"; then
    PHP_SUBST(SAMPLE_SHARED_LIBADD)
    PHP_NEW_EXTENSION(sample, sample.c, $ext_shared)
fi
  • PHP_ARG_ENABLE:產生 --enable-sample 選項,無外部依賴用 enable;有依賴用 PHP_ARG_WITH (產生 --with-sample)。
  • PHP_NEW_EXTENSION:宣告擴展名稱、源檔列表、共享模組標記。

三、php_sample.h —— 介面定義

#ifndef PHP_SAMPLE_H
#define PHP_SAMPLE_H

#define PHP_SAMPLE_EXTNAME "sample"
#define PHP_SAMPLE_EXTVER  "1.0"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"  // 核心 API 總入口

extern zend_module_entry sample_module_entry;
#define phpext_sample_ptr &sample_module_entry

#endif
  • 防重複包含守衛。
  • HAVE_CONFIG_H:phpize 建構時由 config.h 定義。
  • php.h 包含核心所有公共 API 頭文件。
  • zend_module_entry 宣告為 extern,供 dlopen()/dlsym() 尋找入口。

四、sample.c —— 最小實作

#include "php_sample.h"

zend_module_entry sample_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
    STANDARD_MODULE_HEADER,
#endif
    PHP_SAMPLE_EXTNAME,
    NULL,                    // Functions (function_entry 陣列)
    NULL,                    // MINIT
    NULL,                    // MSHUTDOWN
    NULL,                    // RINIT
    NULL,                    // RSHUTDOWN
    NULL,                    // MINFO
#if ZEND_MODULE_API_NO >= 20010901
    PHP_SAMPLE_EXTVER,
#endif
    STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_SAMPLE
ZEND_GET_MODULE(sample)  // 共享模組導出符號
#endif

五、編譯安裝驗證流程

  1. cd php-src/ext/sample
  2. phpize(生成 configure、Makefile 等建構檔)。
  3. ./configure --enable-sample --enable-debug --enable-maintainer-zts
  4. make
  5. make install(輸出 .so 至擴展目錄)。
  6. 編輯 php.ini 加入 extension=sample.so。
  7. php -m | grep sample 驗證。

六、關鍵宏說明

七、下一步

在 function_entry 陣列加入自訂函式,實作 PHP_FUNCTION(),即可開始真正的擴展功能開發。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
宏用途
STANDARD_MODULE_HEADER填充 size、zend_api、zend_debug、zts 等標準欄位
STANDARD_MODULE_PROPERTIES填充 version、globals_size、globals_id 等屬性
ZEND_GET_MODULE共享模組必須導出 get_module() 函式,供 dl() 載入
COMPILE_DL_SAMPLE共享模組編譯時由建構系統定義