Required parameter $xxx follows optional parameter $yyy

34,815

Solution 1

This style of function declaration has been deprecated in PHP 8.0. Writing functions like this has never made sense, since all parameters (up to the last required one) would need to be specified when the function was called. It also caused confusion with use of the ReflectionFunctionAbstract class to analyze functions and methods.

The new deprecation simply ensures that function signatures follow the common sense assumption that required parameters, which must appear, should always be declared before optional ones.

The function should be rewritten to remove the default value on the earlier parameters. Since the function could never be called without declaring all parameters, this should have no effect on its functionality.

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

Solution 2

The required parameter without a default value should come first.

function test_function(int $xxx, int $yyy = 2)
{
    return $xxx * $yyy;
}
 
Share:
34,815

Related videos on Youtube

miken32
Author by

miken32

Programmer since (in a manner of speaking) 1988, systems and network administrator, open source contributor, old guy. He/him.

Updated on March 02, 2022

Comments

  • miken32
    miken32 over 2 years
    Deprecated: Required parameter $xxx follows optional parameter $yyy in...
    

    Since upgrading to PHP 8.0 this error is thrown when running code like this:

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

    This has worked in past versions of PHP without issue.

    • Darren Murphy
      Darren Murphy about 3 years
      Change the order of the parameters, placing $xxx ahead of the optional parameter $yyy.
    • miken32
      miken32 about 3 years
      There is an answer to the question already. Doing what you suggest would mean rewriting every use of your function across your code base.
  • Rain
    Rain over 3 years
    "since all parameters (up to the last required one) would need to be specified when the function was called" . I disagree 3v4l.org/Nb6i6 ;)
  • miken32
    miken32 over 3 years
    Very impressive nitpicking! 👍🏼
  • Enverex
    Enverex about 3 years
    "Writing functions like this has never made sense" - I disagree. There are often occasions where arguments may be ordered differently for sanity or readability reasons. This deprecation seems like an unnecessary and annoying change.
  • miken32
    miken32 about 3 years
    @Enverex the default value specified in such a case could never be used, so it serves no purpose. I've been programming professionally in PHP for more than 15 years and I've never needed to do it. Argument order and default values are not intended for use as documentation, that's what PHPDoc is for.
  • Enverex
    Enverex about 3 years
    "the default value specified in such a case could never be used" Yes it could, you pass null for values you wish to use the default. Also I'm not talking documentation here, just friendlier argument ordering.
  • miken32
    miken32 about 3 years
    @Enverex If you pass null you get null for the parameter, not the default value. 3v4l.org/MGWIR
  • miken32
    miken32 about 3 years
    And then update every usage of that method throughout your code?
  • Alemoh Rapheal Baja
    Alemoh Rapheal Baja about 3 years
    I don't think you'll have to do so except it throws an error.
  • chiliNUT
    chiliNUT about 3 years
    @Rain what php8 feature is your example using? Assuming it must be a new php8 feature since its a syntax error in the previous versions.
  • Rain
    Rain about 3 years
    @chiliNUT Named Arguments.
  • Alemoh Rapheal Baja
    Alemoh Rapheal Baja about 3 years
    the parameter that is required without a default value should come first
  • Ray Perea
    Ray Perea almost 3 years
    @Rain - I disagree too. My 2 cents... This PHP8 deprecation wasn't well thought out at all. In PHP8, they added named parameters which means that you can now call parameters in any order as long as you call them by name like this: func(param1: "value1", param2: "value2") So, that syntax compliments being able to specify default values for any parameter in any order. @miken32 - You said "since all parameters (up to the last required one) would need to be specified when the function was called" - This isn't true anymore
  • miken32
    miken32 over 2 years
    Every place you use that method you would need to change your code. Why would you do this instead of just setting a default on the second parameter?
  • Omkar Ghurye
    Omkar Ghurye over 2 years
    @miken32 "If a parameter with a default value is followed by a required parameter, the default value has no effect." This is deprecated as of PHP 8.0.0 and can generally be resolved by dropping the default value or by changing the position of parameters as suggested above, without a change in functionality.
  • miken32
    miken32 over 2 years
    It completely changes the function; now you have to find and rewrite every single line of code that uses this function. My question was, why is this better than just dropping the default value, which involves no changes to other code? Not to mention this subpar approach has already been given in another answer.