Conversation
9474411 to
c5de589
Compare
Zend/zend_compile.c
Outdated
| default_node.op_type = IS_UNUSED; | ||
| op_array->required_num_args = i + 1; | ||
| if (have_optional_param) { | ||
| zend_error(E_COMPILE_WARNING, "Required parameter $%s follows optional parameter", |
There was a problem hiding this comment.
Maybe the message should also say this will become an error in a later PHP version. (If that is the plan)
|
Per the ML discussion, I've changed this to throw a deprecation warning instead. |
|
This looks controversial to me, there should be an RFC. |
This is not to remove this altogether, it's to deprecate the fact that you have a default value on some parameters before some which don't, meaning that you have an optional param before mandatory ones. |
|
I stand corrected, that shouldn't be controversial 👍 |
|
And what if an optional param doesn't have a type? /**
* @param mixed|null $param
* @param $param2
*/
function test($param = null, $param2) |
This case is also covered and would be deprecated. |
What's the alternative for that though? There is no edit: we can do |
The alternative is to have your optional parameter in second place instead of first. |
|
You can't swap arguments willy nilly, that would cause BC breaks. |
There will be a full PHP major cycle for libraries to fix this. |
|
|
60b7d5f to
b63436a
Compare
…tructors (see #4065) Description ----------- Fixes #4055 This PR swaps the first two parameters of the `AsContentElement` and `AsFrontendModule` constructors. This was not released yet, so no BC issue. Background info: As of PHP8.0 having optional parameters before mandatory ones is deprecated. Here is an explanation why the current code did not trigger a deprecation warning and behaved just like two mandatory parameters: php/php-src#5067 (comment) Commits ------- 9058274 fix order of parameters 6df2476 adjust tests 5bd86a5 use miscellaneous as the default type and fix variadic argument type hint 4356ed6 prevent PHP<8.0 jobs from autoloading test cases with PHP8 syntax 8a1d2d8 add 'mixed' type hint for variadic attributes 1f396f9 improve comment 942fc37 Update core-bundle/tests/DependencyInjection/ContaoCoreExtensionTest.php e413bf3 🦊
…tructors (see #4065) Description ----------- Fixes #4055 This PR swaps the first two parameters of the `AsContentElement` and `AsFrontendModule` constructors. This was not released yet, so no BC issue. Background info: As of PHP8.0 having optional parameters before mandatory ones is deprecated. Here is an explanation why the current code did not trigger a deprecation warning and behaved just like two mandatory parameters: php/php-src#5067 (comment) Commits ------- 90582748 fix order of parameters 6df2476e adjust tests 5bd86a50 use miscellaneous as the default type and fix variadic argument type hint 4356ed66 prevent PHP<8.0 jobs from autoloading test cases with PHP8 syntax 8a1d2d82 add 'mixed' type hint for variadic attributes 1f396f9f improve comment 942fc372 Update core-bundle/tests/DependencyInjection/ContaoCoreExtensionTest.php e413bf33 🦊
…rning With this default value, I get a warning like: Deprecated: Required parameter $meta_key follows optional parameter $object_id More information: php/php-src#5067
This addresses https://bugs.php.net/bug.php?id=53399.
Historically, having an "optional" parameter before a required one was useful for poor man's nullable types. That is, one could write
function test(FooBar $param = null, $param2)to get an effective?FooBartypehint on PHP versions that did not natively support it.Since we've had nullable types since PHP 7.1 now, having a required parameter after an optional one is increasingly likely a bug rather than an intentional workaround, so we should throw a warning for this case.