Skip to content

Defining ini entries

Chung Leong edited this page Nov 17, 2026 · 3 revisions

An extension's settings are stored in the Extension struct. They're accessed through getter and setter functions:

const php = @import("php/root.zig");
const getter = php.IniEntry.getter;
const setter = php.IniEntry.setter;

pub fn @"call show_ini_entries"(self: *@This()) !void {
    php.print("boolean = {}\n", .{self.boolean});
    php.print("integer = {d}\n", .{self.integer});
    php.print("float = {d}\n", .{self.float});
    php.print("string = {s}\n", .{self.string});
}

pub const @"get boolean" = getter(@This(), .boolean);
pub const @"set boolean" = setter(@This(), .boolean);

pub const @"get integer" = getter(@This(), .integer);
pub const @"set integer" = setter(@This(), .integer);

pub const @"get float" = getter(@This(), .float);
pub const @"set float" = setter(@This(), .float);

pub const @"get string" = getter(@This(), .string);
pub const @"set string" = setter(@This(), .string);

boolean: bool = false,
integer: isize = 0,
float: f64 = 0,
string: []const u8 = "",
<?php

ini_set('ini_basic.boolean', true);
ini_set('ini_basic.integer', 1234);
ini_set('ini_basic.float', M_PI);
ini_set('ini_basic.string', "Hello world");

show_ini_entries();
boolean = true
integer = 1234
float = 3.1415926535898
string = Hello world

IniEntry.getter() and IniEntry.setter() are helper functions that generate basic getters and setters. Setters created by the latter do not perform any validation. They also do not replicate strings, unlike setters created by Object.setter(). There's no need as PHP hangs onto settings it received through ini_set().

Ini entries appear automatically in the phpinfo page:

phpinfo

Automatic getters and setters

If all fields in Extension are meant to be used as ini settings, you can ask ZPX to generate getters and setters automatically using the @"user auto getters" and @"user auto setters" declarations:

const php = @import("php/root.zig");

pub fn @"call show_ini_entries"(self: *@This()) !void {
    php.print("boolean = {}\n", .{self.boolean});
    php.print("integer = {d}\n", .{self.integer});
    php.print("float = {d}\n", .{self.float});
    php.print("string = {s}\n", .{self.string});
}

pub const @"use auto getters" = true;
pub const @"use auto setters" = true;

boolean: bool = false,
integer: isize = 0,
float: f64 = 0,
string: []const u8 = "",
<?php

ini_set('ini_auto.boolean', true);
ini_set('ini_auto.integer', 1234);
ini_set('ini_auto.float', M_PI);
ini_set('ini_auto.string', "Hello world");

show_ini_entries();
boolean = true
integer = 1234
float = 3.1415926535898
string = Hello world

Validation

In order to check for and reject invalid settings, you have to define your own setters. They would override the automatic ones:

const php = @import("php/root.zig");

pub const @"use auto getters" = true;
pub const @"use auto setters" = true;

pub fn @"set string"(self: *@This(), value: []const u8) !void {
    if (value.len > 10) return error.TooLong;
    self.string = value;
}

boolean: bool = false,
integer: isize = 0,
float: f64 = 0,
string: []const u8 = "",
<?php

ini_set('ini_verify.string', "Hello world");
PHP Warning:  too long in /home/cleong/zpx/test/wiki/ini-verify/test.php on line 3

Enum and packed struct

You may use enums and packed structs as extension settings:

const php = @import("php/root.zig");

pub fn @"call show_ini_entries"(self: *@This()) !void {
    php.print("speed = {}\n", .{self.speed});
    php.print("flags = {}\n", .{self.flags});
}

pub const @"use auto getters" = true;
pub const @"use auto setters" = true;

const Speed = enum { slow, faster, fastest };
const Flags = packed struct {
    is_good: bool = false,
    is_fast: bool = false,
    is_cheap: bool = false,
};
pub const @"export Speed" = php.Enum.Custom(Speed);
pub const @"export Flags" = php.Class.Custom(Flags);

speed: Speed = .slow,
flags: Flags = .{},
<?php

ini_set('ini_enum.speed', Speed::fastest->value);
ini_set('ini_enum.flags', Flags::is_good | Flags::is_cheap);

show_ini_entries();
speed = .fastest
flags = .{ .is_good = true, .is_fast = false, .is_cheap = true }

ini_set() currently does not accept enums as input. That's why we have to explicitly reference an enum object's value property.

Controlling where settings can be set

By default, settings can be altered in php.ini and through ini_set(). You can disable the latter by appending " (system)" to the setter declaration:

const php = @import("php/root.zig");

pub const @"use auto getters" = true;
pub const @"use auto setters" = true;

// override auto setter
pub const @"set string (system)" = php.IniEntry.setter(@This(), .string);

boolean: bool = false,
integer: isize = 0,
float: f64 = 0,
string: []const u8 = "",
<?php

if (ini_set('ini_restricted.integer', 1234) === false) {
    echo "could not set integer\n";
}
if (ini_set('ini_restricted.string', "Hello world") === false) {
    echo "could not set string\n";
}
could not set string

Clone this wiki locally