Skip to content

Enum Improvement Proposal #18

@prolic

Description

@prolic
  1. Ignore all Constants beginning with "__"
    This is first at all used by the enum class itself (see point 2) and for extensions of an enum class, where an additional constant shouldn't belong to the enum behaviour.

  2. add three constants:
    a) __FROM_VALUE
    b) __FROM_NAME
    c) __FROM_ORDINAL

  3. change the get method:
    static public function get($value, self::__FROM_VALUE)

This way you can create enums by ordinal or by constant name.

  1. About getConstants()
    the method name indicates an implementation detail of the enum class. This is not usefull in terms of an enum.
    Better we have:
    getNames() and getValues()

Additional: We could perhabs readd a toArray() method, where you get the name => value map again, which is currently what getConstants() does.

This again leave to two conclusions:
a) __toString() shoudldn't be final. An "Enum-like-Html-Object" perhabs could need to override the toString() method and rely an getValue() where needed instead.
Having __toString() returning the value is a good default behaviour.
b) toArray() shoudln't be final, too. The output should be changeable. Rely on the other methods instead, if you need ensured behaviour.
c) what should toArray return? Only what currently getConstants() does? Or perhabs also what the default value is and what the current value is?

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions