A minimal PHP extension built with phpz — the simplest possible starting point for new projects.
- Functions:
hello()prints a greeting,greet(string $name): stringreturns a personalized message - Class:
Counter— constructor with optional initial value,add(int $n),dec(int $n),value(): int
.
├── build.zig # Build config
├── build.zig.zon # Dependencies
├── skeleton.h # C header (includes phpz.h + arginfo)
├── skeleton.stub.php # Stub file → gen_stub.php → arginfo.h
├── skeleton_arginfo.h # Generated arginfo (do not edit)
├── run-tests.php # PHP test runner
├── src/
│ ├── root.zig # Entry: module setup, hello(), greet()
│ └── classes/
│ └── counter.zig # Counter class
└── tests/
├── hello.phpt
├── greet.phpt
└── counter.phpt
# Build and run tests
zig build testCustom PHP include path:
zig build test -Dphp-include-dir=/usr/local/include/php# Build
zig build
# Run functions
php -dextension=./modules/skeleton.so -r 'hello();'
php -dextension=./modules/skeleton.so -r 'echo greet("World");'
# Use the Counter class
php -dextension=./modules/skeleton.so -r '
$c = new Counter(5);
$c->add(3);
echo $c->value();
'Copy this directory and rename skeleton to your extension name throughout:
build.zig.zon→.namebuild.zig→ext_nameskeleton.h→ rename file and update#includeguards +phpext_*_ptrskeleton.stub.php→ rename file, update functions/classessrc/root.zig→ updatemodule(.name = ...)
Then run gen_stub.php to regenerate the arginfo header and you're ready to go.