Skip to content

Defining enumerations

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

Use an `@"export ..." declaration to export an enum defined in Zig to PHP:

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

const Fruit = enum {
    apple,
    banana,
    cherry,
    durian,
};
pub const @"export Fruit" = php.Enum.Custom(Fruit);
<?php

print_r(Fruit::cases());
Array
(
    [0] => Fruit Enum:int
        (
            [name] => apple
            [value] => 0
        )

    [1] => Fruit Enum:int
        (
            [name] => banana
            [value] => 1
        )

    [2] => Fruit Enum:int
        (
            [name] => cherry
            [value] => 2
        )

    [3] => Fruit Enum:int
        (
            [name] => durian
            [value] => 3
        )
)

Case aliases

When a constant of the enum is of the same type (i.e. @This()), that constant is automatically exported. It'd act as an alias for the case it refers to:

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

const Fruit = enum {
    apple,
    banana,
    cherry,
    durian,

    pub const favorite: @This() = .banana;
};
pub const @"export Fruit" = php.Enum.Custom(Fruit);
<?php

print_r(Fruit::banana);
print_r(Fruit::favorite);
Fruit Enum:int
(
    [name] => banana
    [value] => 1
)
Fruit Enum:int
(
    [name] => banana
    [value] => 1
)

Other constants have to be exported explicitly using a @"export ..." declaration.

Instance and static methods

Like custom classes, custom enums can have methods attached to them. A notable difference is that the self argument is not a pointer here:

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

const Fruit = enum {
    apple,
    banana,
    cherry,
    durian,

    pub fn @"call show"(self: @This()) void {
        php.print("Fruit #{d}: {s}\n", .{ @backingInt(self) + 1, @tagName(self) });
    }
};
pub const @"export Fruit" = php.Enum.Custom(Fruit);
<?php

Fruit::cherry->show();
Fruit #3: cherry

Static methods are also possible. As in the case of static class methods, the self pointer points to a separate static struct:

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

const Fruit = enum {
    apple,
    banana,
    cherry,
    durian,

    pub fn @"static call showAll"(self: *Static) void {
        const en = @typeInfo(@This()).@"enum";
        inline for (en.field_names, en.field_values, 0..) |name, value, i| {
            php.print("Fruit #{d}: {s}{s}\n", .{
                value + 1,
                name,
                if (self.special == i) " <-- special" else "",
            });
        }
        // make the next fruit the special one
        self.special += 1;
        if (self.special >= en.field_names.len) self.special = 0;
    }

    const Static = struct {
        special: usize = 1,
    };
};
pub const @"export Fruit" = php.Enum.Custom(Fruit);
<?php

Fruit::showAll();
echo "---\n";
Fruit::showAll();
Fruit #1: apple
Fruit #2: banana <-- special
Fruit #3: cherry
Fruit #4: durian
---
Fruit #1: apple
Fruit #2: banana
Fruit #3: cherry <-- special
Fruit #4: durian

Defining custom classes

Clone this wiki locally