Phpz — Write PHP extensions in Zig

Documentation

Hi everyone! Back in July, I shared phpz, my framework for writing native PHP extensions in Zig, in the monthly project thread. I’ve since released version 0.1.0, with support for PHP 8.6.

Main features

  • PHP stubs — Declare functions and classes in a .stub.php file, just as you would for a PHP extension written in C. Implement them in Zig.
  • Arguments and return values — phpz uses Zig’s comptime to generate argument parsing and return value conversions from the Zig function signature.
  • PHP classes and objects — Implement a PHP class with a Zig struct. phpz binds methods by name and lets you customize how each object’s instance data is initialized, cloned, and released.
  • Type safety — phpz provides type-safe Zig wrappers for Zend types such as zval, strings, arrays, objects, references, and resources.
  • Memory allocation — Use PHP’s request allocator through Zig’s std.mem.Allocator interface.
  • Cross-compilation — Cross-compile PHP extensions for Linux, macOS, and Windows.

phpz also supports constants, module globals, INI settings, lifecycle hooks, bailout-safe operations, and observers for function calls, errors, and exceptions.

Experimental support for custom SAPIs lets you embed the PHP engine in a Zig program, run PHP scripts, and control input and output.

phpz supports Zig 0.17.0 and PHP 8.2–8.6 (NTS and ZTS) on Linux, macOS, and Windows.

Example

Declare the hello and greet functions and the Counter class in a .stub.php file.

<?php

/** @generate-class-entries */
namespace Demo;

function hello(): string {}
function greet(string $name): string {}

class Counter {
    public function __construct(int $n = 0) {}
    public function add(int $n): void {}
    public function value(): int {}
}

Implement them in Zig.

const std = @import("std");
const phpz = @import("phpz");

const functions = struct {
    pub fn hello() []const u8 {
        return "Hello from Zig!";
    }

    pub fn greet(name: []const u8) !*phpz.zend.String {
        var buffer: [256]u8 = undefined;
        const result = try std.fmt.bufPrint(&buffer, "Hello, {s}!", .{name});
        return .init(result, false);
    }
};

const Counter = struct {
    n: i64 = 0,

    pub fn __construct(n: ?i64) Counter {
        return .{ .n = n orelse 0 };
    }

    pub fn add(self: *Counter, n: i64) void {
        self.n +|= n;
    }

    pub fn value(self: *const Counter) i64 {
        return self.n;
    }
};

comptime {
    phpz.functions(functions, .{ .namespace = "Demo" });
    phpz.module(.{
        .name = "demo",
        .version = "0.1.0",
        .classes = &.{phpz.Class("Demo\\Counter", Counter, .{})},
    });
}

Load the extension and call it from PHP.

echo Demo\hello();       // Hello from Zig!
echo Demo\greet("Zig");  // Hello, Zig!

$counter = new Demo\Counter(40);
$counter->add(2);
echo $counter->value();  // 42

AI / LLM usage disclosure

I designed and developed the core of phpz myself. I used AI primarily to test ideas early in the project. It also helped with tasks such as writing GitHub Actions workflows, generating test cases, and translating the Chinese documentation into English.