A Zig framework for building PHP extensions with PHP C API bindings.
- zig: 0.17.0
- php: 8.2-8.6 (NTS/ZTS) on Linux (x86_64/aarch64), macOS (x86_64/aarch64), Windows MSVC (x64)
- Modules — extension metadata, lifecycle hooks, phpinfo, INI, globals, observers, and exports.
- Stub integration — arginfo, function entries, constants, attributes, class metadata, and arginfo checks.
- Functions and methods — typed
Ctxarguments, returns, nullable values, raw zvals, and PHP argument errors. Scalar parameters follow ZPP exactly: weak-mode coercion,strict_types, int-to-float widening, null deprecations and Zend's own error messages. - Classes and OOP — bind Zig
structlifecycles to PHP objects, with class entries, methods, inheritance, interfaces, enums, exceptions, handlers, and optional operator overloading and comparison. - Runtime state — typed module globals, PHP/Zend globals, and superglobal access for NTS/ZTS builds.
- Zval and Zend APIs — zval conversions, ownership helpers, arrays (including
ListBuilderforZEND_HASH_FILL_PACKED-style construction andsymtable*lookups with PHP key semantics), strings, objects, callables, functions, and resources. - Native closures — create PHP
Closurevalues withFunction.createClosure(),closure.createFromHandler(), orclosure.createFromFn(). - INI, phpinfo, and errors — typed INI values, phpinfo helpers, PHP errors, exceptions, and bailout-safe cleanup.
- Observers — function-call, error, and exception hooks for profiling and monitoring.
- Memory — PHP-backed Zig allocator for any alignment, with optional debug leak traces that are safe under ZTS.
- Build integration — PHP C translation, extension linking, and platform-specific setup.
- PHP build system — optional support for built-in and shared extensions. Pass
--with-php-build-systemtophpz_skel.php; see the skeleton guide. - Cross-compilation — build Linux (glibc or musl), macOS and Windows extensions with the target PHP SDK.
Generate an extension with the phpz 0.1.0 skeleton generator.
curl -fsSL https://github.com/happystraw/phpz/releases/download/0.1.0/phpz_skel.php | \
php -- --ext my_php_extensionOn PHP 8.2, download the script and run it locally.
curl -fsSLO https://github.com/happystraw/phpz/releases/download/0.1.0/phpz_skel.php
php phpz_skel.php --ext my_php_extensionTo run the generated project's tests:
cd my_php_extension
zig build run-testsFor a complete minimal project, see
examples/skeleton.
zig fetch --save git+https://github.com/happystraw/phpzA phpz extension usually contains:
build.zig.zon— declares thephpzdependencybuild.zig— initializesPhpzand callsphpz.addExtension<name>.stub.php— PHP API declarations<name>_arginfo.h— generated by PHP'sgen_stub.php<name>.h— includesphpz.hand the generated arginfo headersrc/root.zig— registers functions, classes, and the module
php /path/to/php-src/build/gen_stub.php <name>.stub.phpzig build -Dphp-include-dir="$(php-config --include-dir)"For the complete build.zig, stub.php, header, Zig module, and PHPT tests, use examples/skeleton as the reference.
Check out the examples/ directory for complete working examples.
Build-time Compile-time Runtime
────────── ──────────── ───────
stub.php Zig source
│ │
│ php-src/build/gen_stub.php │
▼ │
arginfo.h │
[ext_functions] │
[register_class_*] │
[register_{name}_symbols] │
│ │
│ translate-c (zig build) │
▼ ▼
PHP C bindings ─────────────► phpz comptime:
(php_c module) ├─ phpz.function()/functions() → zif_* export
├─ phpz.Class() → wrapper + zim_* exports
└─ phpz.module() → get_module() export
│
├─── PHP loads .so
│
▼
module_startup [auto]
├─ register_{name}_symbols (constants)
├─ ini_entries
└─ user hook
└─ Class.register() (classes) [manual]
-
Build-time —
gen_stub.phpgeneratesarginfo.hfrom.stub.php, containing function metadata andregister_*symbols.translate-cconverts PHP C headers into Zig bindings. -
Compile-time —
phpz.function()andphpz.functions()exportzif_*wrappers,phpz.Class()creates wrapper types, andphpz.module()exportsget_module()for the dynamic loader. -
Runtime — PHP loads
.so→get_module():What Registered by When How Functions ext_functionsModule init Auto: set in module entry struct Constants register_{name}_symbolsMINIT Auto: called by module_startup_funcClasses register_class_*MINIT Auto: .classesofphpz.module, in the given orderFunctions are resolved from the module entry when the extension loads; constants are registered during MINIT via the auto-generated
register_{name}_symbols. Classes listed in.classesare registered in order beforemodule_startup_fn, so list parents and interfaces first; use a class's.registeroption to supply them.
phpz was inspired by ext-php-rs.