-
-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strict types renders env() unusable #878
Comments
I'd rather solve this differently, for example by explicitly saying the env value should be casted to |
As mentioned in the issue, casting |
I think this is what you're proposing: While if we let PHP do it's thing: We end up with a more correct implementation. |
No, I meant something like: ->constructorParameter('port', \DI\env( 'DATABASE_PORT' )->asInt()) And |
Ah, yes, that would be beautiful |
@mnapoli, do you have time to implement this or should I create a PR? |
I will not implement this myself, so feel free to tackle this. |
Strict types hide and mask a lot of errors if not used correctly (
((int) null) === 0 && ((string) null === '')
for example, where anull
would -- and probably should -- be an error). At the same time, PHP coercion is exceptionally well-documented and predictable. This is our reason for not using strict types, but I understand many people live by it.Env variables are strings (at least in Linux) by default and will be passed as such. However, there's no way to cast these strings to ints, and due to strict types, they will not be coerced as expected:
This will fail to be constructed unless the programmer accepts a
string
forport
, which would be pretty silly. Strict types apply on the caller, not the receiver, and only applies to scalar values as object values are always strict by default.Since strict types only applies to scalar values and only when calling a function from that file, there are only several function calls where this applies in the entire file
ObjectCreator.php
, one of those being the constructor itself. It doesn't do any reflection actually to validate/cast the type either, so this will naturally fail:This library would be far more flexible and valuable to avoid strict types in this file, as it doesn't have much-realized value.
The text was updated successfully, but these errors were encountered: