Declarations and registration

Module registration

Declare the extension entry point at compile time with phpz.module:

const extension = @import("extension_info");

comptime {
    phpz.functions(functions, .{});
    phpz.module(.{
        .name = extension.name,
        .version = extension.version,
        .classes = &.{CounterClass},
    });
}

The project chooses the Zig identifiers functions and CounterClass. The PHP function, method and class names exposed by the bindings must match their stub declarations. For example, the first argument to phpz.Class sets the PHP class name for CounterClass. See Class registration options. The skeleton reads the extension name and version from extension_info.

Module registration options

Use phpz.module to export module symbols for a regular extension. When a custom SAPI needs to register an additional module at PHP startup, use phpz.initModuleEntry to create its entry. See Additional modules for a custom SAPI. Both APIs accept the following compile-time configuration. Only .name is required.

OptionDefaultPurpose
.nameRequiredPHP extension name as a zero-terminated string, such as "my_php_extension".
.version"0.0.0"Extension version string returned by phpversion("my_php_extension") for the example above.
.classesnullClass wrappers to register in order, such as &.{CounterClass}. List parents and interfaces before their dependents.
.resourcesnullResource types to register, such as &.{ValueResource}. See the example under Registration order.
.ini&.{}INI entries to register. Entries bound to module globals must use the same wrapper type as .globals.
.globalsnullThe type returned by phpz.ModuleGlobals(T). Each TSRM thread gets its own instance under ZTS. See Module globals.
.observernullZend observer configuration. Registration is skipped when the extension is loaded temporarily with dl().

The five optional lifecycle hooks all default to null. That section lists their phases and signatures.

Registration order

PHP discovers functions through the module’s function table. The phpz MINIT wrapper processes these entries in order:

  1. The generated module symbol registration function, if present, registers module constants and other symbols.
  2. The .ini settings.
  3. The resource types in .resources, in array order.
  4. The classes in .classes, in array order.
  5. The .observer configuration, except for modules loaded temporarily with dl().
  6. .module_startup_fn.

Register each class only once through .classes. Resources are registered before classes so constructors and methods can create them. List parents and interfaces before dependent classes.

.resources accepts types returned by phpz.zend.Resource.Typed(T, .{ .name = ..., .destroy = ... }). Module startup assigns each resource type a process-local ID valid until module shutdown. Request-local resources are released with the request. See PHP resources.

const Value = struct { number: i64 };

fn destroy(value: *Value) void {
    phpz.heap.php_allocator.destroy(value);
}

const ValueResource = phpz.zend.Resource.Typed(Value, .{
    .name = "my_php_extension.value",
    .destroy = destroy,
});

comptime {
    phpz.module(.{
        .name = extension.name,
        .version = extension.version,
        .resources = &.{ValueResource},
        // Other settings, including .classes, omitted
    });
}

Lifecycle hooks

OptionPhasePurpose
module_startup_fnMINITInitialize module state
module_shutdown_fnMSHUTDOWNRelease module resources
request_startup_fnRINITReset state for this request
request_shutdown_fnRSHUTDOWNClean up at request end
info_fnMINFODisplay extension information

The first four hooks have the signature fn () anyerror!void. info_fn takes a *phpz.ModuleEntry. Keep request-allocated PHP values within their request. For per-thread module state, see Module globals.

See Lifetime boundaries for cleanup responsibilities across calls, requests, objects and module globals.

Additional modules for a custom SAPI

In examples/custom_sapi, the host calls phpz.initModuleEntry(.{ .name = "custom_sapi_app" }) and stores the entry in the global variable addon_module. PHP registers the module when the host calls try custom_sapi.startup(&addon_module). PHP scripts can then call the host-provided function custom_sapi_greeting().

Keep the entry’s storage alive through module shutdown. See Custom SAPI for the startup and shutdown sequence.