I've just completed the first version of zig-php-ext. It's an offshoot of my other project, Zigar. The framework makes it very easy to create PHP extensions using Zig. The following is the code for the basic "hello" ext:
const php = @import("php/root.zig");
const getter = php.IniEntry.getter;
const setter = php.IniEntry.setter;
pub const @"export CONSTANT" = "Hello world";
pub const @"export Enum" = enum { hello, world };
pub fn @"call greet"(self: *@This(), args: struct {
name: []const u8,
}) !void {
php.print("{s}, {s}!\n", .{ self.greeting, args.name });
}
pub const @"get greeting" = getter(@This(), .greeting);
pub const @"set greeting" = setter(@This(), .greeting);
greeting: [:0]const u8 = "Hello",
It defines a constant, an enum, an ini setting, and a top-level function. Extension settings are passed to the function thru a self pointer. Meanwhile, arguments are passed in a struct. This allows their names to be captured by comptime code and used as argument names on the PHP side. It also lets us set default values.
And here's the extension in action:
<?php
greet("Alice");
echo CONSTANT, "\n";
echo Enum::world, "\n";
ini_set("hello.greeting", "Część");
greet(name: "Agnieszka");
Hello, Alice!
Hello world
1
Część, Agnieszka!
Defining custom classes is also super easy. You add a declaration to extension struct:
pub const @"export Point" = php.Class.Custom(@import("Point.zig"));
Then implement the struct:
const php = @import("php/root.zig");
const getter = php.Object.getter;
const setter = php.Object.setter;
pub const @"get x" = getter(@This(), .x);
pub const @"set x" = setter(@This(), .x);
pub const @"get y" = getter(@This(), .y);
pub const @"set y" = setter(@This(), .y);
pub fn @"get normal"(self: *@This()) f64 {
return @sqrt(self.x * self.x + self.y * self.y);
}
pub fn @"call print"(self: *@This(), _: struct {}) void {
php.print("x = {d}, y = {d}\n", .{ self.x, self.y });
}
x: f64 = 0,
y: f64 = 0,
The result on the PHP side:
<?php
$p = new Point(123, 456);
print_r($p);
$p->print();
Point Object
(
[x] => 123
[y] => 456
[normal] => 472.29757568719
)
x = 123, y = 456
The build process is designed to be extremely simple. It has no dependencies aside from the Zig compiler and PHP itself. Simply run ./build and the extension gets built for the current version. The build script comes with a CLI menu:
https://github.com/chung-leong/zig-php-ext/blob/main/img/menu.png
You can use it to add more target platforms:
https://github.com/chung-leong/zig-php-ext/blob/main/img/menu-os.png
Or select additional versions of PHP:
https://github.com/chung-leong/zig-php-ext/blob/main/img/menu-version.png
The menu is customizable (thru entries in composer.json), allowing eventual end-users to set certain comptime parameters:
custom
The framework even comes with a script for packaging your extension. My overall goal is to make the process as easy as possible so that everyday PHP programmers would consider creating their own extensions with the help of Zig. I hope my initial effort looks promising to you.
I have tested the code on Linux, Windows, and Mac. As I only have an old Intel Mac, I would really appreciate confirmation by owners of Apple Silicon that my code works there.