On Mon, Jun 14, 2021 at 4:42 PM Marco Pivetta <ocram...@gmail.com> wrote:
> Hey NikiC, > > On Mon, Jun 14, 2021, 16:35 Nikita Popov <nikita....@gmail.com> wrote: > >> Hi internals, >> >> With the introduction of enums, it's now possible for constants to hold >> objects. However, this works only when using the `const X = Y` syntax, not >> when using `define('X', Y)`, which still excludes objects. >> >> I've submitted https://github.com/php/php-src/pull/7149 to relax the >> restriction. This means that define() would accept everything apart from >> recursive arrays. >> >> An alternative here would be to allow define() to only accept enum objects >> in particular, but not other objects. I would prefer not to do that, as >> such a restriction would be rather arbitrary, now that the technical work >> to support objects has been done. (PS: Please keep the difference between >> mutability and interior mutability in mind.) >> > > I rarely encounter any code using `define()` for anything more than > extremely minimal environment details these days, and even then, it usually > leads to loading an immutable `Environment` object that is then passed > through dependency injection (container or not) to runtime services. > > What are the possible use-cases for this, besides enumeration types? > Not sure about specific use cases, for me the important aspect here is to avoid arbitrary restrictions. For example, define() accepts resources, and this is used for some core constants like STDIN, STDOUT, STDERR. In the future, we're going to switch these resources to be objects, so allowing them in constants would be necessary at that point at least. Actually, we already encountered this as a BC issue when switching some other resource types to objects. I don't think there is any principled argument for why constants would accept arbitrary resources but not arbitrary objects. There's only the historical circumstance that objects weren't supported for technical reasons, which have since been addressed. Regards, Nikita