Optional parameter $yyy precedes required parameter $xxx
P粉236743689
P粉236743689 2023-10-18 12:51:21
0
2
705

Deprecated: Required parameter $xxx follows optional parameter $yyy in...

Since upgrading to PHP 8.0, this error will be thrown when running the following code:

function test_function(int $var1 = 2, int $var2) {
    return $var1 / $var2;
}

This worked without issue in past versions of PHP.

P粉236743689
P粉236743689

reply all(2)
P粉021553460

Required parameters without default values ​​should come first.

function test_function(int $xxx, int $yyy = 2)
{
    return $xxx * $yyy;
}
P粉427877676

This method of function declaration has been deprecated in PHP 8.0. It never makes sense to write a function like this because all arguments (until the last required one) need to be specified when calling the function. It's also using the Causing confusion ::getNumberOfRequiredParameters" rel="noreferrer">ReflectionFunctionAbstract class to parse functions and methods.

The new deprecation simply ensures that function signatures follow the common-sense assumption that required parameters that must be present should always be declared before optional parameters.

This function should be rewritten to remove the default values ​​for earlier parameters. Since the function can never be called without declaring all arguments, this should have no effect on its functionality.

function test_function(int $var1, int $var2) {
    return $var1 / $var2;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template