-
Notifications
You must be signed in to change notification settings - Fork 7.8k
null default-value promoted class parameter before required parameter is silently ignored #11488
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
Comments
Deprecation notice would be added before Fatal error, so expected result should be:
|
Fair point, in-terms of clarity, I'll update it. I was debating not including the function call at all, but it helps illustrate the context better. |
I was about to classify this as a feature, but noticed that this already works correctly for Lines 6958 to 6960 in 6c015ae
Constructor property promotion fails for a different reason, as stated in the RFC: https://wiki.php.net/rfc/constructor_promotion
The RFC requires an explicit |
… null type The check would only work for the ?type syntax, but not type|null. Switch to a check during type compilation instead. Fixes phpGH-11488
… null type The check would only work for the ?type syntax, but not type|null. Switch to a check during type compilation instead. Fixes phpGH-11488
#11497 I still targeted master for this. The fix was not completely trivial, the bug is non-crucial and 8.3 will still allow fixing the error before removal (if that's even planned). |
Description
The following code:
Resulted in this output:
But I expected this output instead:
To explain; $a does not end up being optional, and if you perform reflection on the parameter and/or property, neither parameter/property has a default-value. So PHP is silently ignoring the user-specified behavior, which only surfaces once you do the call to the function (or as it is sometimes in our case, it doesn't show up at all because some objects are created via reflection).
Further information
Normal functions and methods support "poor mans nullable types" for now, which makes the above case a weird quirk of the backwards compatibility, as noted by @KapitanOczywisty in this post #11485 (comment).
However, constructors with parameter/property promotion behave in a stricter manner.
C3 proves that poor mans nullable types aren't intended to apply to constructors with typed promoted parameters/properties like they do for normal parameters, and C5 proves that it's deprecated for them regardless. And C2 and C4 proves that optional parameters aren't allowed before required parameters and emits a deprecation.
However, C1 emits no warning at all, even though parameter/property doesn't end up having a null default-value, nor is it optional. So C1 behaves identically to not specifying a default-value at all, whereas the only purpose of specifying the null default-value is to make it optional, which it isn't.
So it definitely seems like C1 should emit a deprecation as well.
PHP Version
PHP 8.2.7
Operating System
No response
The text was updated successfully, but these errors were encountered: