Declarations and registration

Function bindings

Bind functions

Declare the PHP functions in the stub and keep the skeleton’s generator annotations at the top:

<?php
/**
 * @generate-legacy-arginfo 80200
 * @generate-class-entries
 * @undocumentable
 */
namespace MyPHPExt;
function byteCount(string $value): int {}

After generating arginfo, implement the functions in a Zig container. These snippets assume the project imports phpz and registers the module:

const functions = struct {
    pub fn byteCount(value: []const u8) usize {
        return value.len;
    }
};

comptime {
    phpz.functions(functions, .{ .namespace = "MyPHPExt" });
}

functions binds all public functions in the container. Set .namespace to the PHP namespace prefix or omit it for the global namespace. Once loaded, MyPHPExt\byteCount("Zig") returns 3.

To map functions individually, use several Zig containers, or bind different Zig and PHP names, replace the comptime block with namedFunctions:

comptime {
    phpz.namedFunctions(.{
        .@"MyPHPExt\\byteCount" = functions.byteCount,
    });
}

Choose one form and cover all concrete stub functions in one call. Explicit @alias declarations reuse their targets. Match fully qualified PHP names exactly, including case. Keep helpers private when binding automatically.

For bindings spread across Zig modules, export individual handlers with phpz.function("MyPHPExt\\byteCount", functions.byteCount). It checks that function’s declaration and handler as a batch binding would. It cannot detect missing implementations elsewhere. Prefer a batch binding when functions can be registered together.

Handler signatures

For byteCount(value: []const u8) usize, phpz reads the PHP string as a borrowed byte slice. Returning value.len writes its length to the PHP result as an integer. You can use the PHP parameter name from the stub in a named call:

use function MyPHPExt\byteCount;

var_dump(byteCount(value: "Zig")); // int(3)
var_dump(byteCount("é"));          // int(2) (UTF-8)

If you change the Zig parameter to ?[]const u8 and handle omission in the function body while the stub still declares a required string, the build fails because the required counts differ. Passing an array to the correctly built function instead raises a PHP TypeError at the call site.

Declare the Zig types corresponding to the PHP parameters and return T or !T. The wrapper parses arguments and sets the PHP result. Optional Ctx or GuardCtx precedes PHP parameters. On a backed instance method, put self before the context.

A handler without context or typed parameters checks for an empty call. A context-only handler checks arguments itself using expectNoArgs() or expectArgs(). With void or !void use ctx.ret() to set the result manually. With other return types use the Zig return value. The stub supplies the PHP signature and reflection metadata.

At compile time, typed handlers check the following parts of generated arginfo that affect memory safety:

  • Required and fixed parameter counts and the trailing variadic flag must agree.
  • A fixed by-reference parameter takes *phpz.zend.Reference, its optional form, or a raw *phpz.Zval that preserves the reference. A fixed by-value parameter cannot take the reference type. A by-reference variadic tail (mixed &...$args) takes phpz.Variadic.
  • For direct returns, a by-reference declaration requires *phpz.zend.Reference and a by-value declaration excludes it. Context handlers returning void or !void write the slot themselves and are responsible for the reference semantics.
  • PHP 8.4+ @frameless-function declarations fail at the binding site because phpz does not export frameless handlers.

Check specific PHP types and defaults against the stub yourself. For context-only handlers, expectArgs checks counts in Debug builds.

Functions, methods and closures follow the rules in Argument parsing and return values. For native resource cleanup, see Bailouts and cleanup.