Repository navigation
Receiving arguments
Arguments are transported from PHP inside a container struct. This struct is typically the second argument of the handler function. The use of a struct allows argument names to be captured at comptime. This information can then be used by PHP to enable argument passing by name:
const php = @import("php/root.zig");
pub fn @"call foo"(_: *@This(), args: struct {
x: f64,
y: f64,
z: f64,
}) !void {
php.print("x = {d}, y = {d}, z = {d}\n", .{ args.x, args.y, args.z });
}<?php
foo(1, 2, 3);
foo(z: 3, y: 2, x: 1); // by namex = 1, y = 2, z = 3
x = 1, y = 2, z = 3
The use of a struct also allows arguments to have default values. The presence of a default value makes an argument optional:
const php = @import("php/root.zig");
pub fn @"call foo"(_: *@This(), args: struct {
x: f64,
y: f64,
z: f64 = 3333,
}) !void {
php.print("x = {d}, y = {d}, z = {d}\n", .{ args.x, args.y, args.z });
}<?php
foo(1, 2);x = 1, y = 2, z = 3333
Optional arguments can come before required ones provided that they're of distinct types:
const php = @import("php/root.zig");
pub fn @"call bar"(_: *@This(), args: struct {
boolean: bool = true,
number1: i32,
number2: i32,
}) !void {
php.print("boolean = {}, number1 = {d}, number2 = {d}\n", .{
args.boolean,
args.number1,
args.number2,
});
}<?php
bar(123, 456);boolean = true, number1 = 123, number2 = 456
Zig does not have a dedicated string type. Strings are typically represented by []const u8 or
[:0]const u8, a read-only slice pointer to 8-bit unsigned intergers--with or without a null
terminator (called "sentinel" in Zig lingo):
const php = @import("php/root.zig");
pub fn @"call hello"(_: *@This(), args: struct {
name: []const u8,
}) !void {
php.print("Hello, {s}!\n", .{args.name});
}<?php
hello("world");Hello, world!
String arguments can have default values:
const php = @import("php/root.zig");
pub fn @"call hello"(_: *@This(), args: struct {
name: []const u8 = "world",
}) !void {
php.print("Hello, {s}!\n", .{args.name});
}<?php
hello();Hello, world!
You may choose to accept a PHP string as argument:
const php = @import("php/root.zig");
pub fn @"call hello"(_: *@This(), args: struct {
name: *php.String,
}) !void {
php.print("Hello, {s}!\n", .{args.name.slice()});
}<?php
hello("world");
Hello, world!
The string object can be retrieved from a []const u8 using String.fromOwnSlice():
const php = @import("php/root.zig");
pub fn @"call hello"(_: *@This(), args: struct {
name: []const u8,
}) !void {
const str: *php.String = .fromOwnSlice(args.name);
php.print("address of name = {x}\n", .{
@intFromPtr(args.name.ptr),
});
php.print("address of str.slice() = {x}\n", .{
@intFromPtr(str.slice().ptr),
});
}<?php
hello("world");You can only do this when you know the slice pointer always comes from an actual PHP string. In practice, that means you cannot give the argument a default value.
Define a struct when you wish to receive an object with well defined fields:
const php = @import("php/root.zig");
const Person = struct {
name: []const u8,
profession: []const u8,
age: u16,
};
pub fn @"call show"(_: *@This(), args: struct {
person: Person,
}) !void {
php.print("Name: {s}\nProfession: {s}\nAge: {d}\n", .{
args.person.name,
args.person.profession,
args.person.age,
});
}<?php
class Person {
var $name;
var $profession;
var $age;
function __construct($name, $profession, $age) {
$this->name = $name;
$this->profession = $profession;
$this->age = $age;
}
};
$person = new Person("Ralph Wiggum", "Politician", 24);
show($person);Name: Ralph Wiggum
Profession: Politician
Age: 24
The same function will accept an associative array:
<?php
show([ "name" => "Ralph Wiggum", "profession" => "Politician", "age" => 24 ]);
Use a slice pointer when you want to receive an array of things:
const php = @import("php/root.zig");
const Person = struct {
name: []const u8,
profession: []const u8,
age: u16,
};
pub fn @"call show"(_: *@This(), args: struct {
persons: []Person,
}) !void {
for (args.persons, 0..) |person, i| {
php.print("#{d}.\nName: {s}\nProfession: {s}\nAge: {d}\n", .{
i + 1,
person.name,
person.profession,
person.age,
});
}
}<?php
class Person {
var $name;
var $profession;
var $age;
function __construct($name, $profession, $age) {
$this->name = $name;
$this->profession = $profession;
$this->age = $age;
}
};
show([
new Person("Ralph Wiggum", "Politician", 24),
new Person("Lisa Simpson", "Physicist", 25),
new Person("Nelson Muntz", "Psychologist", 26),
]);#1.
Name: Ralph Wiggum
Profession: Politician
Age: 24
#2.
Name: Lisa Simpson
Profession: Physicist
Age: 25
#3.
Name: Nelson Muntz
Profession: Psychologist
Age: 26
To receive data stored inside a custom object, use a pointer to the struct type that was given to
Class.Custom():
const php = @import("php/root.zig");
const Example = struct {
opaque_ptr: *anyopaque,
};
pub const @"export Example" = php.Class.Custom(Example);
pub fn @"call create"(_: *@This()) !Example {
return .{ .opaque_ptr = @ptrFromInt(0xDEADBEEF) };
}
pub fn @"call receive"(_: *@This(), args: struct {
object: *Example,
}) void {
php.print("opaque_ptr = 0x{X}\n", .{
@intFromPtr(args.object.opaque_ptr),
});
}<?php
$object = create();
receive($object);opaque_ptr = 0xDEADBEEF
When an argument can be of different types, use a tagged union:
const php = @import("php/root.zig");
pub fn @"call show"(_: *@This(), args: struct {
variant: union(enum) {
integer: i64,
float: f64,
boolean: bool,
},
}) !void {
switch (args.variant) {
.integer => |i| php.print("integer = {d}\n", .{i}),
.float => |f| php.print("float = {d}\n", .{f}),
.boolean => |b| php.print("boolean = {}\n", .{b}),
}
}<?php
show(123);
show(M_PI);
show(false);integer = 123
float = 3.141592653589793
boolean = false
Note: Beware of automatic data conversion. In the example above, if float is listed in front of
integer in the struct definition, 123 will get matched to float since integers can be stored
in a floating-point number.
You can use a union type in situation where an argument can be an instance of different custom classes:
const php = @import("php/root.zig");
const Cat = struct {
pub fn say(_: *@This()) void {
php.print("meow\n", .{});
}
};
const Dog = struct {
pub fn say(_: *@This()) void {
php.print("woof\n", .{});
}
};
const Fox = struct {
pub fn say(_: *@This()) void {
php.print("????\n", .{});
}
};
pub const @"export Cat" = php.Class.Custom(Cat);
pub const @"export Dog" = php.Class.Custom(Dog);
pub const @"export Fox" = php.Class.Custom(Fox);
pub fn @"call poke"(_: *@This(), args: struct {
animal: union(enum) {
cat: *Cat,
dog: *Dog,
fox: *Fox,
},
}) !void {
switch (args.animal) {
inline else => |animal, tag| {
php.print("{s}: ", .{@tagName(tag)});
animal.say();
},
}
}<?php
poke(new Dog);
poke(new Cat);
poke(new Fox);
try {
poke(new stdclass);
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}dog: woof
cat: meow
fox: ????
poke(): argument #1 ($animal) is incorrect - not Cat, Dog, or Fox
Named arguments that do not match the name of any declared argument are treated as though they're an associative array given as the last argument. This argument must be a struct and it must be optional (i.e. it must have a default value). This mechanism allows the end user to specify options that aren't commonly used with the help of named arguments:
const php = @import("php/root.zig");
pub fn @"call show_options"(_: *@This(), args: struct {
name: []const u8,
options: struct {
param_a: bool = false,
param_b: i32 = 0,
param_c: enum { good, bad, ugly } = .good,
} = .{},
}) !void {
php.print("options = {}\n", .{args.options});
}<?php
show_options("hello");
show_options("hello", param_a: true);
show_options("hello", param_b: 123, param_c: 'ugly' );
show_options("hello", [ 'param_b' => 123, 'param_c' => 'ugly' ]);options = .{ .param_a = false, .param_b = 0, .param_c = .good }
options = .{ .param_a = true, .param_b = 0, .param_c = .good }
options = .{ .param_a = false, .param_b = 123, .param_c = .ugly }
options = .{ .param_a = false, .param_b = 123, .param_c = .ugly }
As you can see, the use of named arguments has the same effect as using an associative array. Thanks to it the code is bit more compact and easier to read.
See section on variadic function if you want to get all unmatched named arguments.
Use a pointer to Value if your function needs a by-ref argument:
const php = @import("php/root.zig");
pub fn @"call set_text"(_: *@This(), args: struct {
value: *php.Value,
}) !void {
args.value.release();
args.value.* = .fromString(.static("Hello world"));
}<?php
set_text($text);
echo $text, "\n";Hello world
Value represents a PHP variant--a.k.a. zval.
If you wish to define a function accepting any number of arguments, you need to add the special
@"..." field to the argument struct:
const php = @import("php/root.zig");
pub fn @"call list_arguments"(_: *@This(), args: struct {
arg0: u32,
@"...": []const php.Value,
}) !void {
php.print("arg0 = {d}\n", .{args.arg0});
for (args.@"...") |arg| php.print("{}\n", .{arg.kind()});
}<?php
list_arguments(1234, 1, 2.5, "Hello world", false, null, (object) []);arg0 = 1234
.integer
.float
.string
.boolean
.null
.object
Add @"???" to the argument struct if you wish to get named arguments as well:
const php = @import("php/root.zig");
pub fn @"call list_arguments"(_: *@This(), args: struct {
arg0: u32,
@"...": []const php.Value,
@"???": ?*php.Array,
}) !void {
php.print("arg0 = {d}\n", .{args.arg0});
for (args.@"...") |arg| php.print("{}\n", .{arg.kind()});
if (args.@"???") |named| {
var iter = named.iterate(.{});
while (iter.next()) |n_arg| {
php.print("{s} = {}\n", .{ iter.key().string().slice(), n_arg.kind() });
}
}
}<?php
list_arguments(4567, false, cow: 'tasty', chicken: 1234);arg0 = 4567
.boolean
cow = .string
chicken = .integer