skip to Main Content
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.

5

Answers


  1. Chosen as BEST ANSWER

    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;
    }
    

  2. The required parameter without a default value should come first.

    function test_function(int $xxx, int $yyy = 2)
    {
        return $xxx * $yyy;
    }
     
    
    Login or Signup to reply.
  3. "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

    1. by dropping the default value
    2. by changing the position of parameters as suggested above

    , without a change in functionality.

    This method worked in my case =)

    I was facing the following error :

    ErrorException Required parameter $id follows optional parameter $getLink

    Following code was generating this Exception

    public function fo($getLink = null , $id)
    { ......
    }
    

    To Solve this error, I Changed the position of the parameters as suggested in the following code :

     public function fo($id, getLink = null)
        { ......
        }
    

    Done =)

    Login or Signup to reply.
  4. the solution that worked fine for me was interchanging the position of the parameters ensuring no parameter comes after any parameter with the default value set to null.

    Login or Signup to reply.
  5. If anybody is trying to find a way of spotting functions that need updating, you can use this regular expression in linux via grep which will output the results to a file called results.txt. It will only search inside .php files.

    Took me a while to get it to work and it probably needs more tweaking!

    flag -i = Case insensitive search
    flag -n = print line number match was found on with the file found
    flag -r = recursive from current location downwards
    flag -E = Use extended regular expression rules
    
    
    grep -irnE --include *.php 'function ([a-zA-Z0-9_]*?)[ ]?(.*?$[a-zA-Z0-9_]*?=.*?($[a-zA-Z0-9_]*?)[),]{1}[^{]*?' > results.txt
    

    Hope this helps anyone trying to find and correct errors.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search