Required parameter $originalDate follows optional parameter $format

While declaring a function or a method, adding a required parameter after optional parameters is deprecated since PHP 8.0 version. You have to use the required parameter (first parameter) before the optional parameters while defining the function.

Answers 1
  • PHP 8.0: Deprecate required parameters after optional parameters in function/method signatures

    //Function definition with error
    function changedateFormat($format = 'd-m-Y', $originalDate){
        return date($format, strtotime($originalDate)); 
    }
    
    //Sample code with solution which helps you to avoid error Required parameter follows optional parameter.
    function changedateFormat($originalDate, $format = 'd-m-Y'){
        return date($format, strtotime($originalDate)); 
    }
    function changedateFormat(string $format, $originalDate){
        return date($format, strtotime($originalDate)); 
    }
    function changedateFormat(string $format = null, $originalDate){
        return date($format, strtotime($originalDate)); 
    }
    
    0

    PHP documentation already explains that having required parameters after optional parameters is incorrect. There was no deprecation notice until the PHP 8.0 version. You can change the order of parameters and use the (type of parameter) string before the optional parameter or you can also pass the type of parameter with null value to solve the error.

  • Back to code snippet queries related php