PHP and Enumerations

后端 未结 30 1537
有刺的猬
有刺的猬 2020-11-22 13:39

I know that PHP doesn\'t have native Enumerations. But I have become accustomed to them from the Java world. I would love to use enums as a way to give predefined values whi

30条回答
  •  忘了有多久
    2020-11-22 14:09

    I found this library on github and I think it provides a very decent alternative to the answers here.

    PHP Enum implementation inspired from SplEnum

    • You can type-hint: function setAction(Action $action) {
    • You can enrich the enum with methods (e.g. format, parse, …)
    • You can extend the enum to add new values (make your enum final to prevent it)
    • You can get a list of all the possible values (see below)

    Declaration

    Usage

    type-hint enum values:

提交回复
热议问题