Repository navigation
Returning values
ZPX will automatically convert your functions' return values to the right PHP types. That is, provided there's a reasonable way to do so. The following types cannot be returned:
- Many and C pointers
- Integer types larger than 64-bit
- "One" pointers pointing to non-struct
- Unions
- Error sets
You can return a static string just like you would an integer or a bool:
const php = @import("php/root.zig");
pub fn @"call get_string"(_: *@This()) ![]const u8 {
return "Hello world";
}<?php
$string = get_string();
var_dump($string);string(11) "Hello world"
On the other hand, special care has to be taken when the string being returned had to be allocated. The following code causes a memory leak:
const php = @import("php/root.zig");
pub fn @"call get_string"(_: *@This(), args: struct {
name: []const u8,
}) ![]const u8 {
// *** DON'T DO THIS ***
return php.allocator.print("Hello, {s}!", .{args.name});
}<?php
$string = get_string("Bob");
var_dump($string);string(11) "Hello, Bob!"
[Fri Nov 13 18:02:51 2026] Script: '/home/rwiggum/zpx/test/retval-string-leak/test.php'
php/Allocator.zig(26) : Freeing 0x00007155534630c0 (11 bytes), script=/home/rwiggum/zpx/test/retval-string-leak/test.php
=== Total 1 memory leaks detected ===
After creating the string from the []const u8 it received, ZPX was unaware that this memory has
to be disposed of. So we end up with a leak.
To correctly return allocated memory, you function handler needs to ask for an allocator:
const std = @import("std");
const php = @import("php/root.zig");
pub fn @"call get_string"(_: *@This(), allocator: std.mem.Allocator, args: struct {
name: []const u8,
}) ![]const u8 {
return allocator.print("Hello, {s}!", .{args.name});
}The presence of the allocator in the argument list establishes who owns the allocated memory. Since it has come from an allocator provided by the caller, the memory belongs to the caller. It's the caller's responsible to free it.
Fixed-length arrays (and vectors) can be returned just like scalars:
const php = @import("php/root.zig");
pub fn @"call get_array"(_: *@This()) [4]i32 {
return .{ 10, 20, 30, 40 };
}<?php
$array = get_array();
print_r($array);Array
(
[0] => 10
[1] => 20
[2] => 30
[3] => 40
)
Variable-length arrays (i.e. slice pointers), on the other hand, need an allocator:
const std = @import("std");
const php = @import("php/root.zig");
pub fn @"call get_array"(_: *@This(), allocator: std.mem.Allocator, args: struct {
len: usize,
}) ![]i32 {
const items = try allocator.alloc(i32, args.len);
for (0..args.len) |i| items[i] = @intCast((i + 1) * 10);
return items;
}<?php
$array = get_array(5);
print_r($array);Array
(
[0] => 10
[1] => 20
[2] => 30
[3] => 40
[4] => 50
)
ZPX deals with a struct in one of two ways. It'll first try to put the struct inside a custom
object. If that's not possible because the class is unavailable, that is, Class.Custom(T) has
not been exported, ZPX will try to convert the struct to a stdclass object. The following
example shows the latter mechanism:
const php = @import("php/root.zig");
const Point = struct {
x: f64,
y: f64,
};
pub fn @"call get_point"(_: *@This()) !Point {
return .{ .x = 123, .y = 456 };
}<?php
print_r(get_point());stdClass Object
(
[x] => 123
[y] => 456
)
You can create an array of stdclass objects by returning a slice holding unexported structs:
const std = @import("std");
const php = @import("php/root.zig");
const Person = struct {
name: []const u8,
profession: []const u8,
};
pub fn @"call get_athletes"(_: *@This(), allocator: std.mem.Allocator) ![]Person {
const persons = try allocator.alloc(Person, 3);
persons[0] = .{
.name = try allocator.dupe(u8, "Joe Montana"),
.profession = try allocator.dupe(u8, "Football player"),
};
persons[1] = .{
.name = try allocator.dupe(u8, "Joe DiMaggio"),
.profession = try allocator.dupe(u8, "Baseball player"),
};
persons[2] = .{
.name = try allocator.dupe(u8, "Joe Frazier"),
.profession = try allocator.dupe(u8, "Boxer"),
};
return persons;
}<?php
print_r(get_athletes());Array
(
[0] => stdClass Object
(
[name] => Joe Montana
[profession] => Football player
)
[1] => stdClass Object
(
[name] => Joe DiMaggio
[profession] => Baseball player
)
[2] => stdClass Object
(
[name] => Joe Frazier
[profession] => Boxer
)
)
For this to work, all fields of the struct must be translatable to native PHP values. A panic would occur at runtime otherwise as in the following example:
const std = @import("std");
const php = @import("php/root.zig");
const Person = struct {
name: [*c]const u8,
profession: [*c]const u8,
};
pub fn @"call get_person"(_: *@This()) !Person {
return .{ .name = "Norman Bates", .profession = "Hotel manager" };
}<?php
print_r(get_person());panic: Struct has not been exported as a custom class and cannot be automatically converted to stdclass: Extension.Person
When a struct type is exported as a custom class, you can create new instances of it simply by returning the said struct:
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
You can return an array of custom objects with the help of an allocator:
const std = @import("std");
const php = @import("php/root.zig");
const Point = struct {
pub const @"use auto getters" = true;
pub const @"use auto setters" = true;
x: f64,
y: f64,
};
pub const @"export Point" = php.Class.Custom(Point);
pub fn @"call create_multiple"(_: *@This(), allocator: std.mem.Allocator, args: struct {
count: usize,
}) ![]Point {
const list = try allocator.alloc(Point, args.count);
for (list, 0..) |*ptr, i| ptr.* = .{
.x = @floatFromInt(i),
.y = @floatFromInt(i),
};
return list;
}<?php
print_r(create_multiple(5));Array
(
[0] => Point Object
(
[x] => 0
[y] => 0
)
[1] => Point Object
(
[x] => 1
[y] => 1
)
[2] => Point Object
(
[x] => 2
[y] => 2
)
[3] => Point Object
(
[x] => 3
[y] => 3
)
[4] => Point Object
(
[x] => 4
[y] => 4
)
)
In situations where the custom object exists already, you should return a pointer to the struct:
const std = @import("std");
const php = @import("php/root.zig");
const Point = struct {
pub const @"use auto getters" = true;
pub const @"use auto setters" = true;
x: f64,
y: f64,
};
pub const @"export Point" = php.Class.Custom(Point);
pub fn @"call create_multiple"(_: *@This(), allocator: std.mem.Allocator, args: struct {
count: usize,
}) ![]Point {
const list = try allocator.alloc(Point, args.count);
for (list, 0..) |*ptr, i| ptr.* = .{
.x = @floatFromInt(i),
.y = @floatFromInt(i),
};
return list;
}
pub fn @"call get_first"(_: *@This(), args: struct {
list: []*Point,
}) ?*Point {
if (args.list.len == 0) return null;
return args.list[0];
}
pub fn @"call get_last"(_: *@This(), args: struct {
list: []*Point,
}) ?*Point {
if (args.list.len == 0) return null;
return args.list[args.list.len - 1];
}<?php
$list = create_multiple(5);
print_r(get_first($list));
print_r(get_last($list));Point Object
(
[x] => 0
[y] => 0
)
Point Object
(
[x] => 4
[y] => 4
)
A tuple is like a fixed-length array except that its elements can be (and usually are) of different types. Returning one is how a function can return multiple values:
const php = @import("php/root.zig");
pub fn @"call get_pair"(_: *@This()) @Tuple(&.{ []const u8, i32 }) {
return .{ "Hello world", 1234 };
}<?php
[ $string, $int ] = get_pair();
var_dump($string);
var_dump($int);string(11) "Hello world"
int(1234)