This is a simple tutorial to create a PHP extension in windows for both Linux and Windows.
It will require some basic knowledge of PHP and C and creating the following files:
config.m4
php_YOUR_EXT_NAME.h
YOUR_EXT_NAME.c
config.w32
YOUR_EXT_NAME.stub.php
Requirements
- PHP 8.1 or higher
- WSL (Windows Subsystem for Linux) or Linux
- Git Bash and CLI (Command Line Interface) Windows Console
- Visual Studio 2019 or newer (Community Edition is sufficient)
- Basic knowledge of C programming
Step 1: Install the PHP SDK and PHP Source
- Download PHP SDK:
Go to the PHP SDK page on GitHub and download the latest PHP SDK for Windows.
- Setup PHP SDK:
Extract the PHP SDK to a preferred location, e.g., C:\php-sdk
.
- PHP Source Code:
Clone the PHP source code or download it from the official PHP Downloads page.
cd C:\php-sdk
git clone -b PHP-8.1 https://github.com/php/php-src.git php-src
- Prepare the Build Environment:
Open the “Developer Command Prompt for VS 2019” as an administrator and navigate to the PHP SDK directory.
cd C:\php-sdk\php-src
Set up the environment for compiling:
php-sdk-bin-tools\bin\phpsdk_setvars.bat
Step 2: Prepare Your Extension Directory
- Create Extension Directory:
Inside the PHP source tree, navigate to the ext
directory and create a new directory for your extension, e.g., hello
.
cd ext
mkdir hello
cd hello
- Create config.w32:
This file is used to configure your extension. Create config.w32
with the following content:
// config.w32
ARG_ENABLE("hello", "Enable Hello extension", "no");
if (PHP_HELLO != "no") { // if enabled
EXTENSION("hello", "hello.c");
}
- Create config.m4:
This file is used to configure your extension. Create config.m4
with the following content:
PHP_ARG_ENABLE(hello, whether to enable the hello extension,
[ --enable-hello Enable hello extension support])
if test "$PHP_EMPTYORVALUE" != "no"; then
PHP_NEW_EXTENSION(hello, hello.c, $ext_shared)
fi
Step 3: Write Your Extension Code
- hello.c:
Create hello.c
inside your extension directory with the following content:
#include "php.h"
// Declaration of a function
PHP_FUNCTION(hello_world);
// Function argument info
ZEND_BEGIN_ARG_INFO(arginfo_hello_world, 0)
ZEND_END_ARG_INFO()
// List of functions provided by this extension
const zend_function_entry hello_functions[] = {
PHP_FE(hello_world, arginfo_hello_world)
PHP_FE_END
};
// Module entry
zend_module_entry hello_module_entry = {
STANDARD_MODULE_HEADER,
"hello", // Extension name
hello_functions, // Function entries
NULL, NULL, NULL, NULL, NULL,
PHP_HELLO_VERSION,
STANDARD_MODULE_PROPERTIES
};
// Implement standard module entry point
ZEND_GET_MODULE(hello)
// Implementation of hello_world
PHP_FUNCTION(hello_world)
{
RETURN_STRING("Hello, world!");
}
Step 4: Compile the Extension (For Windows)
- Generate configure script:
Back in the root of the PHP source directory, generate the configure
script.
buildconf
- Configure Build:
Configure PHP build with your extension enabled:
configure --disable-all --enable-cli --enable-hello=shared
- Compile PHP:
Now compile PHP and your extension:
nmake
Step 5: Compile the Extension (For Linux)
Prepare the Linux Environment
- Open your Linux distribution through the Start menu.
- Update and install essential packages:
sudo apt update && sudo apt upgrade
sudo apt install build-essential autoconf bison re2c php-dev libtool-bin
Compile Your Extension in WSL
- Transfer your extension source code to the Linux environment. You can do this via the Windows file explorer (the Linux file system is accessible under
\\wsl$\<Distribution Name>\home\<username>\
). - Navigate to your project directory in the WSL terminal.
- Prepare the PHP build environment:
phpize
./configure
make
This will compile the extension to a .so
file.
- Install the extension (if testing on Linux or preparing for deployment):
sudo make install
- Configure PHP to use the extension by adding
extension=your_extension.so
to yourphp.ini
.
Step 6: Test the Extension
- Edit php.ini:
Add your extension to the php.ini
file:
extension=hello.dll
// or for Linux
extension=hello.so
- Run PHP:
You can test if your extension loaded correctly by running:
php -m
And to test function:
php -r "echo hello_world();"
This will build the hello_world
function into PHP as a custom extension, callable like any built-in function. Adjust the function definitions and functionality per your actual extension requirements.
Let us know your opinion!