Skip to content

Bootstrapping a new extension

Chung Leong edited this page Nov 17, 2026 · 6 revisions

Go to this project's release page and download the zip file zpx-0.17.1. Extract the directory stored inside to a place of your choosing and give it a new name. Then open composer.json and change the name and version number of the extension.

{
    "name": "mycompany\/hello",
    "description": "A description of my application",
    "version": "0.1.0"
}

The extension name will appear as part of the dynamically linked library's filename. In the example, the name leads to the creation of php_hello.so on Linux (.dll on Windows, .dylib on macOS). When there're dashes in the name, they're replaced with underscores.

This name is also used as the prefix for the extension's settings in php.ini. As such, the name shows up in the phpinfo page.

The company name and description are ignored.

Directory structure

The project root directory initially contains the following:

⚙️ build
📄 build.extra.zig
📄 build.php
📄 build.zig
📄 CHANGELOG.md
📄 composer.json
📄 composer.zig
📄 LICENSE
⚙️ package
📄 package.php
📁 src
📁 vendor

build.php is the build script while build is a bash script that launches it. package.php is the packaging script and is launched by package.

build.zig is the standard Zig build file. It's used by the compiler. You generally do not need to edit it. Edit functions defined in build.extra.zig instead when you need to import third-party modules or add C include paths.

composer.json, in addition to its usual purpose, doubles as the extension's configuration file. It must be present as both build.zig and build.php read it. It may contain special entries for customizing the build menu. Enum types used in the custom menu configuration appear in composer.zig (empty initially).

src is the sub-directory where the extension's source code is stored. It contains the following initially:

📄 Extension.zig
📄 main.zig
📄 main.c
📁 include
📁 php

Extension.zig is the file you need to modify when creating your extension. It contains function handlers and export declarations. The filename is capitalized per Zig convention since the file itself defines a struct type.

main.zig is the extension's root file, its "entry point" so to speak. It's where the custom module is declared:

const std = @import("std");
const config = @import("config");
const php = @import("php/root.zig");

var module: php.Module.Custom(@import("Extension.zig"), .{
    // ext name and version are extracted from composer.json by build.zig
    .name = config.ext_name,
    .version = config.ext_version,
}) = .init();
comptime {
    module.@"export"();
}

pub fn DllMain(
    _: std.os.windows.HINSTANCE,
    _: std.os.windows.DWORD,
    _: std.os.windows.LPVOID,
) std.os.windows.BOOL {
    return if (php.linkWindowsImports()) |_| .TRUE else |_| .FALSE;
}

You generally don't need to edit this file.

main.c contains workaround code that must be compiled as C. You may choose to put your own C functions here.

include is where C header files are stored. It contains a single file initially--c.h. This file gets translated by translate-c. The result will be available as @import("c"). You may add your own include statements in this file (edit getIncludePaths() in build.extra.zig to add an include path).

php contains ZPX's source code. You'll generally only import php/root.zig.


Defining functions | Defining custom classes | Customizing build menu

Clone this wiki locally