Since PHP 8.0, a deprecation warning may appear when using functions with the following declaration pattern:
function test_function(int $var1 = 2, int $var2) { // ... }
This error message is triggered when a required parameter ($var2 in this example) follows an optional parameter ($var1).
Historically, this syntax has been flawed as it required all parameters (up to the last required one) to be specified in function calls, even if they had default values. Additionally, it hindered the use of the ReflectionFunctionAbstract class for function analysis.
To eliminate the deprecation warning, adjust the function declaration to remove default values for earlier parameters. Since these parameters were always required, their functionality should not be impacted.
function test_function(int $var1, int $var2) { // ... }
The above is the detailed content of Why Do Required Parameters After Optional Parameters Cause PHP Deprecation Warnings?. For more information, please follow other related articles on the PHP Chinese website!