4 ms·
Question: does class_exists still return TRUE when given the QCN to an enum? Because it's not possible to instantiate an enum, if one appears in the constructo
by pdenton 5y ago
Question: does class_exists still return TRUE when given the QCN to an enum?
Because it's not possible to instantiate an enum, if one appears in the constructor of a class it'll break auto-wiring. I've written my own IoC containers and one fundamental assumption in them is that when class_exists returns true, it is what it says on the tin. Using a specialized factory would still be possible. I wonder if other containers would break on seeing enums as well, but I hope they fixed this because it's not mentioned in the breaking changes. The documentation for class_exists doesn't mention enum.
- TazeTSchnitzel 5y agoThere's already kinds of classes that can't be instantiated, why would this specifically be a breaking change?
- pdenton 5y agoIf by "kinds" you mean abstract classes, yeah. I just checked, there's no special handling for those since requesting them from the container makes no sense. I suppose a constructor could depend on an abstract type and expect an actual implementation, though. I haven't thought about that use case actually, but it's possible to configure a substitute for this case. Other IoC containers may handle this one better, since you may want to have different implementations depending on the current scope. If you mean classes with private constructors, those are technically illegal. According to the PHP manual, constructors must be declared public. The fact that private constructors work at all should be considered an implementation detail. Breakage occurs when something is said to be a class, yet isn't a class but still appears in the parameter list of a constructor. The container would instantiate a ReflectionClass of whatever is being requested and pass that around. At some point it'll call newInstanceArgs either with configuration-provided arguments or with auto-wired dependencies. The first case could still work, but foregoes auto-wiring.
- francislavoie 5y agoI can't think of any situation you'd put an enum in the container though. Maybe I'm just too tired to think of a situation where that would be helpful. Can you think of one?
- pdenton 5y ago* A database abstraction layer that's instantiated with a certain access level * A hydrator/extractor that changes the type of the value it handles * A task or job that has a certain priority to it
- deleted 5y ago[deleted]
- Ayesh 5y agoInternally, Enums inherit a lot from classes. I wrote an indepth article about this: https://php.watch/versions/8.1/enums#enum-exists https://php.watch/versions/8.1/enums#enum-exists There is an `enum_exists` function that only returns true for Enums. It's fairly unintuitive to combine class_exists with enum_exists to filter out Enums, I agree.
- donatj 5y agoIsn’t that the same behavior/pattern for traits though? At least it’s consistent.
- pdenton 5y agoThat was easy to test, and no. class_exists returns false for traits and they can't be used as parameter types. It throws a TypeError when I do that (even though the instance I pass uses the trait).