Home >Backend Development >PHP Tutorial >Why Does PHP 8.0 Prevent Required Parameters from Following Optional Parameters?
"Required Parameter Must Precede Optional Parameter"
In PHP versions prior to 8.0, function declarations allowed optional parameters to be followed by required parameters. This practice was deprecated in PHP 8.0, resulting in errors like "Required parameter $xxx follows optional parameter $yyy."
Explanation:
This style of function declaration was irrational as it forced all parameters (except the last required one) to be specified during function calls. Additionally, it led to ambiguities when using the ReflectionFunctionAbstract class for function and method analysis.
Solution:
To resolve the deprecation error, rewrite the function to remove default values from earlier parameters, ensuring that required parameters are declared before optional ones:
function test_function(int $var1, int $var2) { return $var1 / $var2; }
By adhering to this rule, function signatures become more logical and follow the expectation that required parameters should precede optional ones.
The above is the detailed content of Why Does PHP 8.0 Prevent Required Parameters from Following Optional Parameters?. For more information, please follow other related articles on the PHP Chinese website!