Repository navigation
Defining custom classes
We use Class.Custom() to define a custom class. It's a parametric type that accepts a struct
type as an argument. This struct defines the custom class's behaviors and contents. Having defined
it, you export the custom class with an @"export ..." declaration in Extension.zig:
const php = @import("php/root.zig");
pub const @"export Point" = php.Class.Custom(struct {
x: f64 = 0,
y: f64 = 0,
});<?php
$point = new Point;
print_r($point);
?>
Point Object
(
)
The example above does not do anything. It appears to be an empty object because the struct has no getter functions attached.
To declare a property getter, we use a @"get ..." declaration. we use a @"set ..."
decalaration to declare a setter:
const php = @import("php/root.zig");
pub const @"export Point" = php.Class.Custom(struct {
x: f64 = 0,
y: f64 = 0,
pub fn @"get x"(self: *@This()) f64 {
return self.x;
}
pub fn @"set x"(self: *@This(), value: f64) void {
self.x = value;
}
pub fn @"get y"(self: *@This()) f64 {
return self.y;
}
pub fn @"set y"(self: *@This(), value: f64) void {
self.y = value;
}
pub fn @"get normal"(self: *@This()) f64 {
return @sqrt(self.x * self.x + self.y * self.y);
}
});<?php
$point = new Point(x: 123, y: 456);
print_r($point);Point Object
(
[x] => 123
[y] => 456
[normal] => 472.29757568719
)
ZPX provides a pair of helper functions for creating simple getters and setters. The code above can be written as follows:
const php = @import("php/root.zig");
const getter = php.Object.getter;
const setter = php.Object.setter;
pub const @"export Point" = php.Class.Custom(struct {
x: f64 = 0,
y: f64 = 0,
pub const @"get x" = getter(@This(), .x);
pub const @"set x" = setter(@This(), .x);
pub const @"get y" = getter(@This(), .y);
pub const @"set y" = setter(@This(), .y);
pub fn @"get normal"(self: *@This()) f64 {
return @sqrt(self.x * self.x + self.y * self.y);
}
});When all fields have getters/setters (as in this example), you can use the @"user auto getters"
and @"user auto setters" declarations to assign them automatically:
const php = @import("php/root.zig");
pub const @"export Point" = php.Class.Custom(struct {
x: f64 = 0,
y: f64 = 0,
pub const @"use auto getters" = true;
pub const @"use auto setters" = true;
pub fn @"get normal"(self: *@This()) f64 {
return @sqrt(self.x * self.x + self.y * self.y);
}
});If the struct implements init(), the function will be called when a custom object is created:
const php = @import("php/root.zig");
pub const @"export Example" = php.Class.Custom(struct {
pub fn init(args: struct {
number1: i32,
number2: i32,
}) @This() {
php.print("init\n", .{});
return .{ .number1 = args.number1, .number2 = args.number2 };
}
number1: i32,
number2: i32,
});<?php
$object = new Example(123, 456);init
Like top-level functions, init() is expected to accept a single struct. This struct would
contain arguments given to the constructor. It can be omitted if the constructor does not have
arguments. The function may return an error union.
You may choose to implement the in-place variant initSelf() instead, if for some reason you need
a pointer to the object being created:
const php = @import("php/root.zig");
pub const @"export Example" = php.Class.Custom(struct {
pub fn initSelf(self: *@This(), args: struct {
number1: i32,
number2: i32,
}) !void {
php.print("initSelf\n", .{});
self.* = .{ .number1 = args.number1, .number2 = args.number2 };
}
number1: i32,
number2: i32,
});<?php
$object = new Example(123, 456);initSelf
If you do not provide an init function, ZPX would synthesize one based the struct's setters:
const php = @import("php/root.zig");
pub const @"export Example" = php.Class.Custom(struct {
pub const @"use auto getters" = true;
pub const @"use auto setters" = true;
number1: i32 = 0,
number2: i32 = 0,
});<?php
$object = new Example(123, 456);
print_r($object);Example Object
(
[number1] => 123
[number2] => 456
)
The constructor arguments have the same order as the setters. When auto setters are used, this is the same order as the fields.
Automatic initialization is only possible when all fields have default values. Otherwise a call
to the constructor would fail with a not permitted error:
const php = @import("php/root.zig");
pub const @"export Example" = php.Class.Custom(struct {
pub const @"use auto getters" = true;
pub const @"use auto setters" = true;
number1: i32 = 0,
number2: i32, // <- no default value
});<?php
$object = new Example(123, 456);Uncaught Exception: not permitted in /home/rwiggum/zpx/test/object-init-test.php:3
When the struct type has a deinit() function, it'd be called when an object gets
garbage-collected:
const php = @import("php/root.zig");
pub const @"export Example" = php.Class.Custom(struct {
pub fn deinit(_: *@This()) void {
php.print("deinit\n", .{});
}
});<?php
$object = new Example();
$object = null;deinit
When deinit() isn't implemented, ZPX would automatically free all compatible pointer fields:
const php = @import("php/root.zig");
pub const @"export Example" = php.Class.Custom(struct {
pub fn init(args: struct {
text: ?[]const u8,
}) !@This() {
return if (args.text) |text| .{
.text = try php.allocator.dupe(u8, text),
} else .{};
}
pub const @"use auto getters" = true;
text: []const u8 = "Default text",
});<?php
$object = new Example();
print_r($object);
$object = new Example("Hello world");
print_r($object);
$object = null;Example Object
(
[text] => Default text
)
Example Object
(
[text] => Hello world
)
The default deinit routine is designed to free fields that're set by auto-setters. If a field is set to its default value, no attempt would be made to deallocate it. That's the case for the first object in the example above.
Object methods are similiar to regular functions. Instead of Extension, the self argument
points to the custom part of the custom object:
const php = @import("php/root.zig");
pub const @"export Point" = php.Class.Custom(struct {
pub fn @"call print"(self: *@This()) void {
php.print("x = {d}, y = {d}\n", .{ self.x, self.y });
}
pub const @"use auto getters" = true;
pub const @"use auto setters" = true;
x: f64 = 0,
y: f64 = 0,
});<?php
$point = new Point(123, 456);
$point->print();x = 123, y = 456
Methods receive their arguments through a struct as in the case of top-level functions. Return
values are likewise automatically converted to the proper PHP types. For instance, if you wish to
return the same object (you're implementing a "fluent" scheme), you just return the self
pointer:
const php = @import("php/root.zig");
pub const @"export Point" = php.Class.Custom(struct {
pub fn @"call scale"(self: *@This(), args: struct {
factor: f64,
}) *@This() {
self.x *= args.factor;
self.y *= args.factor;
return self;
}
pub fn @"call translate"(self: *@This(), args: struct {
delta: f64,
}) *@This() {
self.x += args.delta;
self.y += args.delta;
return self;
}
pub fn @"call print"(self: *@This()) void {
php.print("x = {d}, y = {d}\n", .{ self.x, self.y });
}
pub const @"use auto getters" = true;
pub const @"use auto setters" = true;
x: f64 = 0,
y: f64 = 0,
});<?php
$point = new Point(5, 5);
$point->scale(2)->translate(2)->print();x = 12, y = 12
A static method behave like an instance method except its self pointer points to a
a separately defined struct:
const php = @import("php/root.zig");
pub const @"export Point" = php.Class.Custom(struct {
pub fn @"static call create"(static: *Static) @This() {
defer {
static.next_x += 5;
static.next_y += 5;
}
return .{ .x = static.next_x, .y = static.next_y };
}
const Static = struct {
next_x: f64 = 10,
next_y: f64 = 10,
};
pub const @"use auto getters" = true;
pub const @"use auto setters" = true;
x: f64 = 0,
y: f64 = 0,
});<?php
print_r(Point::create());
print_r(Point::create());
print_r(Point::create());Point Object
(
[x] => 10
[y] => 10
)
Point Object
(
[x] => 15
[y] => 15
)
Point Object
(
[x] => 20
[y] => 20
)
This struct is a singleton. You can access it in an instance method with the help of
Singleton(T).get(). If the struct implements init() and deinit(), they're invoked at
request startup and shutdown respectively:
const php = @import("php/root.zig");
pub const @"export Pet" = php.Class.Custom(struct {
pub fn init(args: struct {
name: []const u8,
}) !@This() {
return .{ .name = try php.allocator.dupe(u8, args.name) };
}
pub fn deinit(self: *@This()) void {
php.allocator.free(self.name);
}
pub fn @"call say"(self: *@This(), args: struct {
text: []const u8,
}) void {
const static = php.Singleton(Static).get();
php.print("{s} the {s}: {s}\n", .{ self.name, static.species, args.text });
}
pub fn @"static call setSpecies"(static: *Static, args: struct {
label: []const u8,
}) !void {
php.allocator.free(static.species);
static.species = try php.allocator.dupe(u8, args.label);
}
const Static = struct {
species: []const u8,
pub fn init() !@This() {
return .{ .species = try php.allocator.dupe(u8, "unknown") };
}
pub fn deinit(self: *@This()) void {
php.allocator.free(self.species);
}
};
name: []const u8,
});<?php
Pet::setSpecies("cat");
$molly = new Pet("Molly");
$molly->say("meow meow");Molly the cat: meow meow
Class constants | Receiving custom objects | Returning custom objects