Skip to content

Exporting classes and enums from within a class

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

When a class or enum is exported from within another class, it would show up in that class's namespace:

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

pub const @"export somewhere\\out\\there\\ThatClass" = php.Class.Custom(struct {
    pub const @"export Child" = php.Class.Custom(struct {
        pub const @"export Pet" = php.Enum.Custom(enum {
            cat,
            dog,
            rabbit,
            goldfish,
        });
    });
});
<?php

use somewhere\out\there\ThatClass;
use somewhere\out\there\ThatClass\Child;
use somewhere\out\there\ThatClass\Child\Pet;

print_r(new ThatClass);
print_r(new Child);
print_r(Pet::cat);
somewhere\out\there\ThatClass Object
(
)
somewhere\out\there\ThatClass\Child Object
(
)
somewhere\out\there\ThatClass\Child\Pet Enum:int
(
    [name] => cat
    [value] => 0
)

That class would also gain a constant holding the full path to the class or enum:

var_dump(ThatClass::Child);
var_dump(ThatClass::Child::Pet);
string(33) "somewhere\out\there\ThatClass\Child"
string(37) "somewhere\out\there\ThatClass\Child\Pet"

Since strings can be used to reference classes in PHP, the class or enum effectively becomes an "inner" type of that class:

print_r(new (ThatClass::Child));
print_r(ThatClass::Child::Pet::dog);
somewhere\out\there\ThatClass\Child Object
(
)
somewhere\out\there\ThatClass\Child\Pet Enum:int
(
    [name] => dog
    [value] => 1
)

Defining custom classes | Defining enumerations

Clone this wiki locally