Build a Basic PHP Extension: Step‑by‑Step Guide
This guide walks you through creating a simple PHP extension, from generating the skeleton with ext_skel.php to understanding each generated file—config.m4, config.w32, php_test.h, test.c, and tests—so you can confidently build and install your own extension.
In this article we introduce how to create a basic PHP extension, covering environment setup, the extension skeleton, and the core PHP kernel data structures and APIs involved.
Creating a simple PHP extension is straightforward: you only need to create a few files. You can do this manually or generate the skeleton using the ext_skel.php script.
<code>$ php php-src/ext/ext_skel.php --ext test --dir .</code>The script is available only in the PHP source distribution, not in binary builds. It creates a directory test containing the extension framework files.
<code>$ cd test
$ ls
config.m4 config.w32 php_test.h test.c tests</code>The generated files have the following roles:
config.m4 : configuration script used by phpize or buildconf to add extension options to the configure command.
config.w32 : Windows‑specific configuration file (will be discussed later).
php_test.h : C header that defines common extension macros; optional for single‑file extensions but useful for multi‑file projects.
test.c : main source file implementing the extension, defining internal functions, classes, and constants.
tests : directory for PHP test scripts (covered in later sections).
These are the essential components of a PHP extension skeleton. The next step is to build and install the extension.
Feel free to leave comments or questions below.
360 Zhihui Cloud Developer
360 Zhihui Cloud is an enterprise open service platform that aims to "aggregate data value and empower an intelligent future," leveraging 360's extensive product and technology resources to deliver platform services to customers.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.