Skip to content

Enums inside classes #22738

Description

@procodix

Description

I wanted to briefly gauge the sentiment here regarding integrated enums in PHP classes, as a preliminary step toward an RFC. I think this would be quite concise.

Proposal: Enums should be embeddable in PHP classes. The goal is to avoid unnecessarily small files while keeping the namespace clean.

When classes work with many constants—perhaps even in groups—enums are a natural choice to define them and make them accessible for others:

public class Test {
    public const string PARAM_ONE = 'one';
    public const string PARAM_TWO = 'tow';
    //... more
    public const string CONST_ONE = 1;
    public const string CONST_TWO = 2;
        //... more
}

Instead of creating files just for the enums (autoloading), like TestParams.php and TestConsts.php, something like this would be nice, which would give them Enums a proper home, too:

public class Test {
    public enum PARAM {
        public const string ONE = 'one';
        public const string TWO = 'tow';
        //... more
    }
    public enum CONST {
        public const string CONST_ONE = 1;
        public const string CONST_TWO = 2;
        //... more
    }
}

The key question will be: how can the enums be made accessible? To avoid having different methods for internal and external access, general access with appropriate visibility would be the right approach:

Test::PARAM::One

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions