Repository navigation
Defining functions
Top-level functions are declared in Extension.zig. Functions must be public and their names must
begin with "call ". To place a space inside an identifier you have to use the @"..." escape
sequence. This special syntax makes it clear which functions should be made available to PHP code
and which aren't.
In the following example, only foobar() can be used in PHP:
const php = @import("php/root.zig");
pub fn foo(_: *@This()) void {
php.print("foo\n", .{});
}
pub fn bar(_: *@This()) void {
php.print("bar\n", .{});
}
pub fn @"call foobar"(_: *@This()) void {
php.print("foobar\n", .{});
}Four other special prefixes are used by ZPX: @"get ...", @"set ...", @"export ..." and
@"use ...".
The name given to @"call ..." may include a namespace:
const php = @import("php/root.zig");
pub fn @"call somewhere\\out\\there\\foobar"(_: *@This()) void {
php.print("foobar\n", .{});
}<?php
use function somewhere\out\there\foobar;
foobar();foobar
Functions meant for PHP must have self as their first argument even if the variable goes unused.
The pointer points to an instance of Extension, which holds the extension's settings:
const php = @import("php/root.zig");
const getter = php.IniEntry.getter;
const setter = php.IniEntry.setter;
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: []const u8 = "Hello",<?php
ini_set("function_self.greeting", "Cześć");
greet("Agnieszko");Cześć, Agnieszko!
The argument struct, on the other hand, can be omitted if the function does not accept arguments.