search
HomeBackend DevelopmentPHP Tutorial如何编写一个独立的 PHP 扩展(译)

本文翻译自 PHP 源码中的 README.SELF-CONTAINED-EXTENSIONS。文中标记了 注 的内容均为自己添加。内容有点老,也挺啰嗦,没讲什么深入的内容,但是可以作为入门学习参考。

独立的 PHP 扩展可以独立于 PHP 源码之外进行分发。要创建一个这样的扩展,需要准备好两样东西:

  • 配置文件 (config.m4)

  • 你的模块源码

接下来我们来描述一下如果创建这些文件并组合起来。

准备好系统工具

想要扩展能够在系统上编译并成功运行,需要准备转以下工具:

  • GNU autoconf

  • GNU automake

  • GNU libtool

  • GNU m4

以上这些都可以从 ftp://ftp.gnu.org/pub/gnu/ 获取。

注:以上这些都是类 Unix 环境下才能使用的工具。

改装一个已经存在的扩展

为了显示出创建一个独立的扩展是很容易的事情,我们先将一个已经内嵌到 PHP 的扩展改成独立扩展。安装 PHP 并且执行以下命令:

$ mkdir /tmp/newext$ cd /tmp/newext

现在你已经有了一个空目录。我们将 mysql 扩展目录下的文件复制过来:

$ cp -rp php-4.0.X/ext/mysql/* .# 注:看来这篇 README 真的需要更新一下了# PHP7 中已经移除了 mysql 扩展部分

到这里扩展就完成了,执行:

$ phpize

现在你可以独立存放这个目录下的文件到任何地方,这个扩展可以完全独立存在了。

用户在编译时需要使用以下命令:

$ ./configure \       [--with-php-config=/path/to/php-config] \       [--with-mysql=MYSQL-DIR]$ make install

这样 MySQL 模块就可以使用内嵌的 MySQL 客户端库或者已安装的位于 MySQL 目录中的 MySQL。

定义一个新扩展

我们给示例扩展命名为 “foobar”。

新扩展包含两个资源文件:foo.c 和 bar.c(还有一些头文件,但这些不只重要)。

示例扩展不引用任何外部的库(这点很重要,因为这样用户就不需要特别指定一些编译选项了)。

LTLIBRARY_SOURCES 选项用于指定资源文件的名字,你可以有任意数量的资源文件。

注:上面说的是 Makefile.in 文件中的配置选项,可以参考 xdebug。

修改 m4 后缀的配置文件

m4 配置文件可以指定一些额外的检查。对于一个独立扩展来说,你只需要做一些宏调用即可。

PHP_ARG_ENABLE(foobar,whether to enable foobar,[  --enable-foobar            Enable foobar])if test "$PHP_FOOBAR" != "no"; then  PHP_NEW_EXTENSION(foobar, foo.c bar.c, $ext_shared)fi

PHP_ARG_ENABLE 会自动设置好正确的变量以保证扩展能够被 PHP_NEW_EXTENSION 以共享模式启动。

PHP_NEW_EXTENSION 的第一个参数是扩展的名称,第二个参数是资源文件。第三个参数 $ext_shared 是由 PHP_ARG_ENABLE/WITH 为 PHP_NEW_EXTENSION 设定的。

请始终使用 PHP_ARG_ENABLE 或 PHP_ARG_WITH 进行设置。即使你不打算发布你的 PHP 模块,这些设置也可以保证让你的模块和 PHP 主模块的接口保持一体。

注:PHP_ARG_ENABLE 和 PHP_ARG_WITH 应该是用于定义模块是动态扩展还是静态编译进 PHP 中,就跟编译 PHP 时使用的 --enabl-xxx 和 --with-xxx 一样。

创建资源文件

ext_skel 可以为你的 PHP 模块创建一些通用的代码,你也可以编写一些基本函数定义和 C 代码来处理函数的参数。具体信息可以查看 READNE.EXT_SKEL。

不要担心没有范例,PHP 中有很多模块供你参考,选择一个简单的点开始,添加你自己的代码。

注:ext_skel 可以生成好基本模块需要的资源文件和配置文件,不需要自己创建。

修改自定义模块

将 config.m4 文件和资源文件放到同一个目录中,然后执行 phpize (PHP 4.0 以上的版本编译 PHP 的时候都安装了 phpize)。

如果你的 phpize 不在系统环境变量中,你需要指定绝对路径,例如:

$ /php/bin/phpize

这个命令会自动复制必需的构建文件到当前目录并根据 config.m4 创建配置文件。

通过以上的步骤,你已经有了一个独立的扩展了。

安装扩展

扩展可以通过以下命令编译安装:

$ ./configure \            [--with-php-config=/path/to/php-config]$ make install

给模块添加共享支持

有时候独立扩展需要是共享的已供其他模块加载。接下来我会解释如何给已经创建好的 foo 模块添加共享支持。

  • 在 config.m4 文件中,使用 PHP_ARG_WITH/PHP_ARG_ENABLE 来设定扩展,这样就可以自动使用 --with-foo=shared[,..] 或 --enable-foo=shared[,..] 这样的指令作为编译参数了。

  • 在 config.m4 文件中,使用 PHP_NEW_EXTENSION(foo,.., $ext_shared) 使扩展可以被构建。

  • 添加以下代码到你的 C 语言资源文件中:

    #ifdef COMPILE_DL_FOOZEND_GET_MODULE(foo)#endif
  • 这一段讲的上面都提到过了,这里只是又强调了一下。

    PECL 网站约定

    如果你打算发布你的扩展到 PECL 的网站,需要考虑以下几点:

  • 添加 LICENSE 或 COPYING 到 package.xml

  • 需要在扩展头文件中定义好版本信息,这个宏会被 foo_module_entry 调用来声明扩展版本:

    #define PHP_FOO_VERSION "1.2.3"
  • Statement
    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
    11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

    Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

    Introduction to the Instagram APIIntroduction to the Instagram APIMar 02, 2025 am 09:32 AM

    Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

    Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

    Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

    Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

    This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

    Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

    Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

    cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

    The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

    12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

    Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

    Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

    The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool