Code Monkey home page Code Monkey logo

Comments (9)

morrisonlevi avatar morrisonlevi commented on June 3, 2024

I agree that PHP needs an enumeration type. However, I feel like if that comes from anywhere besides the PHP core that it's a kludge and hack.

That being said, I'll at least consider this idea and play around with it a bit.

from ardent.

marc-mabe avatar marc-mabe commented on June 3, 2024

I totally agree with you. But as long as PHP doesn't have that implemented in core it's better to have it within a more used and general library.

from ardent.

olekukonko avatar olekukonko commented on June 3, 2024

You have been playing with this for 2 months ... any updated :)

from ardent.

morrisonlevi avatar morrisonlevi commented on June 3, 2024

I did do a bit of playing around with it. In short, I don't feel like any of the options really help. PHP needs enums in the core and I don't think trying to emulate them will be helpful.

I'm still open to suggestions though.

from ardent.

morrisonlevi avatar morrisonlevi commented on June 3, 2024

Enumerations requirements:

  • Must be able to be serialized consistently
  • Must be able to be used in type-hints
  • Must be able to be auto-loaded

Considerations:

  • Can it be used in instanceof?
  • Support casts to number?

from ardent.

morrisonlevi avatar morrisonlevi commented on June 3, 2024

Oops, wrong button. Meant comment, not close.

from ardent.

marc-mabe avatar marc-mabe commented on June 3, 2024

@morrisonlevi

Must be able to be serialized consistently

Should be possible because constants accepting scalar values only - also it is possible to add the serializable interface

Must be able to be used in type-hints

It's implemented as a class which needs to be instantiated - so the name of the class is the type-hint

Must be able to be auto-loaded

Same as above - it's a class name classes will be autoloadable

Can it be used in instanceof?

Same as above - it's an instance of a class and $enumObject instanceof EnumClass works fine

Support casts to number?

This isn't possible as is
Only with a workaround it could be done:

  • Only allow numbers as values - throw an exception if this isn't the case
  • cast the object to a string returning the current number as a string and than case the string into a number
  • $value = (int) (string) $enumObject;
  • But this isn't nice

The above liked implementation allows all scalar types and returns the current const name on casting to a string.
Casting to the current value needs to be done using the __invoke magic method = calling the enum object like a function.

from ardent.

morrisonlevi avatar morrisonlevi commented on June 3, 2024

After more consideration I still think this is really something that needs to be in PHP core and that a user-land implementation isn't useful enough.

from ardent.

rdlowrey avatar rdlowrey commented on June 3, 2024

I agree ... Enums are only useful as part of the language. In any case, you can "fake" enums perfectly well right now with class constants.

from ardent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.