Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter

Description

An optional parameter has a default value, while a required parameter doesn’t. In a method signature, PHP recommends to set the required parameters first, then the optional parameter.

When this is not the case, PHP is unable to understand if an argument has to be assigned to a parameter, or if it can be skipped, and assigned to the next required. When this happens, any required parameter makes previously defined parameter also required.

Example

<?php

function foo($a = 1, $b) {}

?>

Solutions

  • Remove the default value of the early optional parameter.

  • Add a default value of the late required parameter.

In previous PHP versions, this error message used to be Required parameter $%s follows optional parameter $%s.